I am wondering if anyone has experience with creating images in PHP. I am trying to create a thumbnail that is cropped but centered but I can't seem to get the srcX and srcY variables to respond in the function imagecopyresampled(). It seems that the function is using 0 and ignoring the values defined.

If anyone has any ideas please let me know.

<?php

$src_file = "toy_rx3.jpg";
$dest_file = "dest_rx3.jpg";
$thumbw = 100;
$thumbh = 100;

$src_img = imagecreatefromjpeg($src_file); 
$dst_img = imagecreatetruecolor($thumbw,$thumbh);

$quality = 75;

$srcX = 20;
$srcY = 20;

imagecopyresampled($dst_img, $src_img, 0, 0, $srcX, $srcY, 100, 100, 300, 300); 
imagejpeg($dst_img, $dest_file, $quality); 
imagedestroy($src_img); 
imagedestroy($dst_img); 

print("<img src=$dest_file>");

?> 

    I have a premade function that handles all the details of making thumbnails.. Here's how it works:

    $src_file = "toy_rx3.jpg"; 
    $dest_file = "dest_rx3.jpg"; 
    
    $thumbw = 100; 
    $thumbh = 100; 
    $quality = 75;
    
    $src_img = imagecreatefromjpeg($src_file);  
    $dest_img = image_resize($src_img, $thumbw, $thumbh); imagejpeg($dst_img, $dest_file, $quality);
    imagedestroy($src_img);
    imagedestroy($dst_img); function image_resize($im, $maxWidth, $maxHeight) { $maxAspect = $maxWidth / $maxHeight; //Figure out the aspect ratio of the desired limits $origWidth = imagesx($im); //Get the width of the uploaded image $origHeight = imagesy($im); //Get the height of the uploaded image $origAspect = $origWidth / $origHeight; //Get the aspect ratio of the uploaded image if (($origWidth > $maxWidth) || ($origHeight > $maxHeight)) { //See if we actually need to do anything if ($origAspect <= $maxAspect) { //If the aspect ratio of the uploaded image is less than or equal to the target size... $newWidth = $maxHeight * $origAspect; //Resize the image based on the height $newHeight = $maxHeight; } else { //If the ratio is greater... $newWidth = $maxWidth; //Resize based on width $newHeight = $maxWidth / $origAspect; } $om = imagecreatetruecolor($newWidth, $newHeight); //Create the target image imagecopyresampled($om, $im, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight); //actually do the resizing return($om); //Return the result } else { return($im); //Or don't do anything and just return the input. } }

      Thanks for the reply but I have written a function to do the same, but I have posted a simplier one to demonstrate. The problem is that I can't get the srcX and srcY (5th and 6th parameters of imagecopyresampled) to change the offset of where the original image is copied from.

      Have anyone been able to get this to work?

        3 months later

        Majik: Thanks for that code. I am trying to alter it so as to call it from a web page. Do you see any problems with this:

        $filename = $HTTP_GET_VARS['mls'];
        if(file_exists("/home/user/public_html/photos/" . $filename . ".jpg")) {
        	$src_file = "/home/user/public_html/photos/" . $filename . ".jpg";
        } else {
        	$src_file = "/home/user/public_html/assets/nophoto.jpg";
        }
        
        $dest_file = "thumb.jpg";  
        
        $thumbw = 120;  
        $thumbh = 92;
        $quality = 75; $src_img = imagecreatefromjpeg($src_file);
        $dst_img = image_resize($src_img, $thumbw, $thumbh); header("Content-type: image/jpeg");
        imagejpeg($dst_img, $dest_file, $quality);
        imagedestroy($src_img);
        imagedestroy($dst_img); // resize original function image_resize($im, $maxWidth, $maxHeight) {
        $maxAspect = $maxWidth / $maxHeight; //Figure out the aspect ratio of the desired limits $origWidth = imagesx($im); //Get the width of the uploaded image $origHeight = imagesy($im); //Get the height of the uploaded image $origAspect = $origWidth / $origHeight; //Get the aspect ratio of the uploaded image if (($origWidth > $maxWidth) || ($origHeight > $maxHeight)) { //See if we actually need to do anything if ($origAspect <= $maxAspect) { //If the aspect ratio of the uploaded image is less than or equal to the target size... $newWidth = $maxHeight * $origAspect; //Resize the image based on the height $newHeight = $maxHeight;
        } else { //If the ratio is greater... $newWidth = $maxWidth; //Resize based on width $newHeight = $maxWidth / $origAspect;
        }
        $om = imagecreatetruecolor($newWidth, $newHeight); //Create the target image imagecopyresampled($om, $im, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight); //actually do the resizing return($om); //Return the result } else {
        return($im); //Or don't do anything and just return the input. }
        }

        Then call it with : <img src="createthumb.php?filename=80">

        There are no errors - but the image does not display??

          I found a couple of problems with the code:

          1. Your main problem is that you are using the POSTed variable "filename" in your URL, but you are referring to some variale called "mls" in the program.

          2. Since you're outputting the image directly, you don't need the line that specifies a destination file, nor do you want the second two arguments of imagejpeg().

          3. These two are the only killers in the code. With this correction you should get the result you expect. There are a few other items that I recommend cleaning up:

          4. Unless you are using an ancient version of PHP (4.1 or earlier) the HTTP_GET_VARS method is considered deprecated and will likely stop working entirely in within the next few releases. $POST, $GET, $COOKIE, and $REQUEST are the preferred new method. Check them out in the manual.

          5. I make a habit of outputting the header at the very beginning of the code. While this is technically not necessary, it can save you a headache down the road. 🙂

          See if this code helps (my function is not posted for the sake of reducing repetition):

          header("Content-type: image/jpeg"); //If the script is outputting an image I find it helpful to make this the very first line.
          $filename = $_GET['filename'];
          if(file_exists("/home/user/public_html/photos/" . $filename . ".jpg")) {
              $src_file = "/home/user/public_html/photos/" . $filename . ".jpg";
          } else {
              $src_file = "/home/user/public_html/assets/nophoto.jpg";
          }
          
          $thumbw = 120;
          $thumbh = 92;
          $quality = 75;
          
          $src_img = imagecreatefromjpeg($src_file);
          $dst_img = image_resize($src_img, $thumbw, $thumbh);
          
          imagejpeg($dst_img); // When you are outputting to the browser you only need the first argument.
          
          imagedestroy($src_img);
          imagedestroy($dst_img);
          
          

            Thanks alot!. I'm on the road, but will give it a try when I return.

              I posted code to do exactly this in the code critique forum a couple days ago.

                And of course it's not really sexy until it does an Unsharp Mask. 🙂

                  I have a different (equally sexy) function to do that 🙂

                    Write a Reply...