OK guys, I have done really no image related operations with PHP, so be gentle. What I'm trying to accomplish is:

  1. upload an image file from a POSTed form

  2. make sure it's reasonably close to a 3:2 aspect ratio (within about 10%)

  3. resize it to 480x320

  4. save a copy using a naming convention that corresponds to a $SESSION value I already set. (like 0000234501_clear.png, where the numeric portion is the $SESSION value)

  5. watermark (with transparency) and save another copy using the same naming convention as above with a different prefix (like 0000234501_watermarked.png

I don't want someone to just write the code for me, rather I'd like some suggestions as to what order things should be done, and preferred methods/functions for getting this accomplished. GD (2.0.34) is installed and available on the server with support for the filetypes I need. Any and all help would be appreciated.

    Pikachu2000;10946268 wrote:
    1. upload an image file from a POSTed form

    2. make sure it's reasonably close to a 3:2 aspect ratio (within about 10%)

    3. resize it to 480x320

    4. save a copy using a naming convention that corresponds to a $SESSION value I already set. (like 0000234501_clear.png, where the numeric portion is the $SESSION value)

    5. watermark (with transparency) and save another copy using the same naming convention as above with a different prefix (like 0000234501_watermarked.png

    1. Use the link dagon provided - the manual has detailed info and examples.

    2. Use [man]getimagesize/man to retrieve the width and height of the image (note: I believe this is also a good way to make sure that the file uploaded was an image file, too). A few basic math operations later and you can accept or reject the upload based on the aspect ratio.

    3. i. Create a new image resource using [man]imagecreatetruecolor/man.

      ii. Load the uploaded file into an image resource using one of the imagecreatefrom*() functions (use the filename or the IMAGETYPE_XXX constant you get back from [man]getimagesize/man to determine which GD function to use).

      iii. Copy and resize the uploaded image to the new resource using [man]imagecopyresampled/man.

      iv. Output the new, resized image to file using the appropriate image*() function (again, same process as in step ii above to determine which GD function to use).

    4. I believe this is accomplished in step iv above.

    5. While I haven't personally worked with watermarking, I believe there are examples in the PHP manual - most likely in one of the user comments. Either search the online documentation or just do a Google search (e.g. "php watermark image") and I'm sure you'll find some examples.

      Thanks, both of you. I didn't have time to work on this today, but I'll be starting on it in the next day or so . . .

        Hi again. Started working on this a while ago, and I've got something I'm having trouble figuring out. The image file uploads just fine, and print_r($FILES) shows the correct information, including size. I've purposely tried files too big (85k) and too small, but no error is set in my $errors[] array, using the conditional below and with the comparisons changed to intval($FILES['new_certificate']['size']) Any idea as to why this would fail? Am I doing something in the wrong order?

        if($_FILES['new_certificate']['size'] < 4096 || $_FILES['new_certificate']['size'] > 51200 ) {
        	$errors[] = "Uploaded image file must be between 4Kb and 50Kb to be usable.";
        }
        

          Disregard the last post about checking the file's size. I got that part working . . .

            bradgrafelman;10946380 wrote:
            1. While I haven't personally worked with watermarking, I believe there are examples in the PHP manual - most likely in one of the user comments. Either search the online documentation or just do a Google search (e.g. "php watermark image") and I'm sure you'll find some examples.

            It is good PHP provide a lot of built-in function to do image operation but when things start to get tough like say water-marking, it maybe time to turn to other software which excel in image operation. Try ImageMagick! Install and learn the command line option which include doing the water-marking feature.

            Then from PHP, we can use the system command to call the ImageMagick convert <option> to do the water-marking for us.

            We got to be realistic, PHP nor Perl nor C nor Java can give us all the possible APIs for us, we just got to make use of each other to achieve our objectives. In this case PHP call ImageMagick to do water-marking.

            Thanks.

              sohguanh;10946602 wrote:

              It is good PHP provide a lot of built-in function to do image operation but when things start to get tough like say water-marking, it maybe time to turn to other software which excel in image operation. Try ImageMagick! Install and learn the command line option which include doing the water-marking feature.

              Then from PHP, we can use the system command to call the ImageMagick convert <option> to do the water-marking for us.

              We got to be realistic, PHP nor Perl nor C nor Java can give us all the possible APIs for us, we just got to make use of each other to achieve our objectives. In this case PHP call ImageMagick to do water-marking.

              Thanks.

              I don't have the option to install Image Magick. I did, however find a way to watermark on-the-fly with PHP and GD with 13 lines of code, including header().

                Pikachu2000;10946623 wrote:

                I don't have the option to install Image Magick.

                This was actually going to be my response! :p

                While ImageMagick is an awesome tool to have, sometimes you can't depend on it being there since it is a different piece of software completely separate from PHP. Writing the code to use the GD library would at least make it more versatile in the event that you export the code to a different server.

                  bradgrafelman;10946661 wrote:

                  This was actually going to be my response! :p

                  While ImageMagick is an awesome tool to have, sometimes you can't depend on it being there since it is a different piece of software completely separate from PHP. Writing the code to use the GD library would at least make it more versatile in the event that you export the code to a different server.

                  If install a separate piece of Open Source software is not feasible then I presume install a PHP package is do-able ?

                  I did a scan over the weekend and behold PECL PHP has just the package for interfacing to ImageMagick! It is called imagick - Summary Provides a wrapper to the ImageMagick library. Ok this still require ImageMagick to be installed but at least we can use PHP code to do image operation and not needing to fork out and call the binaries isn't it ?

                  So it is now more the case PECL and PEAR should merge as one entity! Why create two separate entities and spread the PHP developers over different camps ? Sometimes certain PHP developers are strong in certain areas and the best of all is to consolidate all of them at a central site isn't it ?

                  Ok deviate from PHP but the above package is worth a look.

                  Thanks.

                    Write a Reply...