Jump to content

Recommended Posts

Hi all

 

I'm using this code to resize an image to produce a thumbnail with the gd library

 

				$src_image = imagecreatefromjpeg($stored_image);
				//
				$new_thumb_width = 85;
				$new_thumb_height = 64; 
				$src_width = imagesx($src_image);
				$src_height = imagesy($src_image);
				//
				if($src_width > $src_height){
					$thumb_w = $new_thumb_width;
					$thumb_h = $src_height*($new_thumb_height/$src_width); 
				}
				if($src_width < $src_height){
					$thumb_w = $src_width*($new_thumb_width/$src_height);
					$thumb_h = $new_thumb_height;
				}
				if($src_width == $src_height){
					$thumb_w = $new_thumb_width;
					$thumb_h = $new_thumb_height;
				}
				//
				$thumb = imagecreatetruecolor($thumb_w, $thumb_h);
				imagecopyresampled($thumb, $src_image,0,0,0,0,$thumb_w, $thumb_h, $src_width, $src_height);
				imagejpeg($thumb, $stored_thumb);
				imagedestroy($src_image);
				imagedestroy($thumb);
//
				echo $thumb_w;
				echo "<br/>";
				echo $thumb_h;


 

I want the thumbnails to be 85 x 64 but they are coming out 85 x 48

 

Can anyone see why this isn't working, or another way I might be able to do it.

 

It's part of a photo gallery where the images could be different sizes but I wanted to keep the thumbnails a uniformed.

 

Any help appreicated.

Link to comment
https://forums.phpfreaks.com/topic/144472-resize-image-gd-library/
Share on other sites

Try

<?php
$src_image = imagecreatefromjpeg($stored_image);

$new_thumb_width = 85;

$new_thumb_height = 64; 

list($width, $height) = getimagesize($src);
    
$thumb_w = $new_thumb_width;

$thumb_h = $new_thumb_height;    

$thumb = imagecreatetruecolor($thumb_w, $thumb_h);

imagecopyresampled($thumb, $src_image,0,0,0,0,$thumb_w, $thumb_h, $width, $height);

imagejpeg($thumb);

imagedestroy($src_image);

imagedestroy($thumb);

echo $thumb_w;

echo "<br/>";

echo $thumb_h;
?>

Your example does a ratio resize, maintaining the original aspect ratio, (no cropping or squeezing of the image), only resize down to the nearest ratio if image height > output ratio height Or image width > output ratio width. Which is the best way to do it so the image looks nice...

You have three options

 

1. resize to a certain width (maintain aspect ration)

2. resize to a certain height (maintain aspect ration)

3. resize to a set width and height (ignoring the aspect ratio)

 

 

option (1, 2) allows the image to maintain its natural look while resizing it, but the height or width will only be determined by the ratio of your (restriction) set height or width, option (3) will ignore the aspect ratio and stretch or squash the image to your (restricted) height and width! So which one do you want?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.