Search code examples
phpcssimagejpegphp-gd

imagecopyresampled not working not sure why


So I'm attempting to use imagecopyresampled to crop out a section of a photo so that I don't have to worry about my users uploading photos larger than intended to my website. Unfortunately I have yet to figure out why imagecopyresampled is basically behaving as though I simply resized the image using CSS. From my understanding it should only copy a section of the image at 0,0 based on the coordinates I've provided to a 325X300 px jpg.

!:example

The top image is the one I'm using imagecopyresampled to generate. My code is as follows. Just trying to understand what I'm doing wrong here because apparently my copy of GD doesn't have imagecrop otherwise I'd probably be using that.

 <html>
 <style>
 .sample{
     width: 325;
     height: 300;
 }
 </style>
 <body>
 <?php
 $image = imagecreatefromjpeg('Image6.jpg');
 $filename = 'Thumbnail_Image6.jpeg';

 $width = 325;
 $height = 300;
 $oldwidth = imagesx($image);
 $oldheight = imagesy($image);
 if( $oldwidth > 325 || $oldheight > 300){
$thumb = ImageCreateTrueColor( 325, 300);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, 325, 300, $oldwidth,    $oldheight);
imagejpeg($thumb, $filename, 100);
echo "<img src='".$filename."'><br>";
echo "<img class='sample' src='Image6.jpg'><br>";
 }
 ?>
 </body>

 </html>

Solution

  • if you're going to crop the image, you don't need to use full image size.

    imagecopyresampled($thumb, $image, 0, 0, 0, 0, 325, 300, 325, 300);