Jump to content
Larry Ullman's Book Forums

Croppping And Resizing A Jpg Image


Recommended Posts

Hi Larry,

 

I have another piece of code that I just can't make work properly.  I have Googled and I think that I'm using the correct code but all I get is a jpg image of the correct size and shape but it is blank.

 

$newname is a relative URL to a large jpg image file that I know is there.  I need a square thumbnail jpg image so I'm trying to crop the original image correctly to a square and also downsize it.  I definitely have the GD library in my PHP.

 

 

 

//  crop and resize the image file
              // Get dimensions of the original image
              
              list($current_width, $current_height) = getimagesize($newname);
               echo "<p><font color='yellow'>Current width is $current_width, current height is $current_height.</font></p>";
              // now work out the x and y coordinates on the original image where we
              // will begin cropping the image
              if ($current_width > $current_height)
                    { // image type = "Landscape"
                $left = ($current_width - $current_height) / 2;
                $top = 0;
                $new_width = $current_width - (2 * $left);
                $new_height = $current_height;
               }
              elseif ($current_height > $current_width)
                    { // image type = "Portrait"
                $top = ($current_height - $current_width) / 2;
                $left = 0;
                $new_width = $current_width;
                $new_height = $current_height - (2 * $top);
               }
              else
               {  // image is square
                $top = 0;
                $left = 0;
               }
              
              // This will be the final size of the image (e.g. how many pixels
              // left and down we will be going)
              $crop_width = (int) $_POST['size'];
              $crop_height = (int) $_POST['size'];
               echo "<p><font color='yellow'>Parameters are left: $left, top: $top, new width: $new_width, new height: $new_height.</font></p>";
              // Resample the image
              $canvas = imagecreatetruecolor($crop_width, $crop_height);
              $current_image = imagecreatefromjpeg($newname);
              imagecopy($canvas, $current_image, 0, 0, $left, $top, $new_width, $new_height);
              // imagecopy($canvas, $current_image, 0, 0, 0, 0, $current_width, $current_height);
              /* the imagecopy parameters are:
              1) Destination image link resource
              2) Source image link resource
              3) x-coordinate of destination point
              4) y-coordinate of destination point
              5) x-coordinate of source point
              6) y-coordinate of source point
              7) Source width
              8) Source height
              */
              header('Content-Type: image/jpeg');
              imagejpeg($canvas, $newname, 100);
         

 

Any help will be appreciated.

 

Cheers from Oz.

Link to comment
Share on other sites

Hmmm... I'm not really sure, but there are a few possibilities:

 

1) The original image is a perfect square, and you're not setting $new_width and $new_height values in that case.

 

2) The size value isn't being properly posted.

 

3) Your image link is wrong. (I know you said you already checked this.)

 

4) Your image is not a jpg/jpeg.

 

Beyond that, it's hard to say.

Just for testing purposes, I pretty much copied your script over to my server and tested it, and it works fine.

I used a publicly-accessible image and assumed a few values, but other than that, my code is the same as yours.

The following is my code. You should be able to run it on your server and get a result:

<?php
  
  $src_url = 'http://i.qkme.me/357o9b.jpg';
  
  list($src_width, $src_height) = getimagesize($src_url);
  
  if ($src_width > $src_height) {
    
    $left_offset = ($src_width - $src_height) / 2;
    
    $top_offset = 0;
    
    $des_width = $src_height;
    
    $des_height = $src_height;
    
  } else if ($src_width < $src_height) {
    
    $left_offset = 0;
    
    $top_offset = ($src_height - $src_width) / 2;
    
    $des_width = $src_width;
    
    $des_height = $src_width;
    
  } else {
    
    $left_offset = 0;
    
    $top_offset = 0;
    
    $des_width = $src_width;
    
    $des_height = $src_height;
    
  }
  
  $des_img = imagecreatetruecolor(450, 450);
  
  $src_img = imagecreatefromjpeg($src_url);
  
  imagecopy($des_img, $src_img, 0, 0, $left_offset, $top_offset, $des_width, $des_height);
  
  header ('Content-Type: image/png');
  
  imagejpeg($des_img);
  
  imagedestroy($des_img);

 

Please let me know if you gain any insights.

Thanks.
Link to comment
Share on other sites

Hi HartleySan,

 

I tried to reply with the code that I finally got to work but it would not let me post my reply.

 

If you have an email address or other means of communication, I will be happy to send you my code.

 

Thanks again for your help.

 

Cheers from Oz.

Link to comment
Share on other sites

Hi HartleySan,

 

Thank you for your interest and willingness to help.  I may not have been clear in what I was trying to accomplish - it was to be able to take any reasonable-sized jpg image of whatever (rectangular) shape and from that create a square thumbnail jpg file of a specified (smallish) size.

 

I found out that my code was sort of working though I did not realise it at the time.  After much trial and error I found that what works for me is to first resize the largish jpg file down to a smaller one and I do this with the SimpleImage class from Simon Jarvis (thanks Simon) and then crop the smaller image by either chopping off equal parts of the left and right sides if it's landscape or chopping off equal parts of the top and bottom if it's portrait.  I then end up with a nice thumbnail with most of the scene in it though small and square.

 

The process is not very efficient as the SimpleImage processing first reads the larger file and then saves it back into its original directory though smaller now.  Then the cropping logic reads the just-created small jpg image file, crops it and then writes the cropped file back out to the original directory though I give it a modified name.

 

It all works though there's probably a more efficient way.

 

Cheers from Oz.

Link to comment
Share on other sites

Yes, it was a bit funny but also a bit embarassing!  It just so happened that the jpg image that I was experimenting with had a patch of pale blue sky in the left hand top corner - it was almost white.  So what I was mis-interpreting was that crop, which looked blank, was not working but in fact it was a perfectly good grop of near-white sky!  Traps for young players!!

 

Thanks again for your interest and help.

 

Cheers from Oz.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...