Jump to content

Imagefilledrectangle


djp1988
 Share

Recommended Posts

Hello everyone, I have a photo upload feature to my site, and when i receive a file i make 3 different size thumbnails, one of them is 130 x 85, that's what my example shows, this is just a portion of the script.So the problem is if i get a photo which is too short, say 500 x 100 pixels, when i come and do the resize for the thumbnails, I get a nasty looking black background where there's no more image to copy, so I'm trying to make that go white, so I thought, after creating the temp. image i could fill it white and then copy over the top of it, thinking that is the copied photo is too small, the "empty" part being copied would just be transparent, therefor leaving me with the white background color i set in the previous step, Does the imagecopyresampled method override the imagefilledrectangle even with an empty part of the image being copied?If not then I don't know why this isn't working.Thanks for your time ! :)

$tmp=imagecreatetruecolor(130,85);$color = imagecolorallocate($tmp, 255, 255, 255);imagefilledrectangle($tmp,0,0,130,85,$color);imagecopyresampled($tmp,$src,0,0,0,0,130,85,$width,$height);	imagejpeg($tmp,"resources/image.jpg",85);

Edited by djp
Link to comment
Share on other sites

Okay, no that should be okay, Here's more of the script, it'll be easier to understand:So i sent the function the height of the original imagewidth of the original imagesource (imagecreatefromjpeg),the file name (ex: $ext = image.jpg), and the destination (/resources/images/image.jpg) $makewidth and $makeheight are the dimensions of the thumbnail, from which i make an empty imageI then get the width difference ratio in $cal_widthAnd i get the corresponding height of the image, using the previous ratio, this means i know the dimensions of the image within the thumbnail, so a vertical photo may be: 130 x 280By comparing the $temp_height with the $makeheight i can know if the height is too big or not for the thumbnail, if it is too big I just do the necessary script, and this works, however the problem is if it is too small, So since the first post I have made a change, so i have an empty image of the correct dimensions, and now instead of copying the image onto it plus an extra part which would be overwriting the white background, I am only copying the part of the image which exiss, so by overriding the $makeheight variable in the else statement, setting it to $temp_height, i should be copying the correct width and max height from the image without copying emptiness. However still I end up with a black background.

function thumbs($width,$height,$src,$ext,$dest_img){	$makewidth = 200;	$makeheight = 130;	$tmp=imagecreatetruecolor($makewidth,$makeheight);	$color = imagecolorallocate($tmp,255,255,255);	$cal_width = $makewidth / $width;	$temp_height = $height * $cal_width;	$correct_height = $height;	if($temp_height >= $makeheight){ // the height is too big		$ratio = $width / $makewidth;		$correct_height = $makeheight * $ratio;	}else{		$makeheight = $temp_height;	}	imagecopyresampled($tmp,$src,0,0,0,0,$makewidth,$makeheight,$width,$correct_height); //make the img		imagejpeg($tmp,"Resources/Trips/thumbs200/".$ext,85);}

Edited by djp
Link to comment
Share on other sites

I think I've got it, I wasn't setting the color properly, I am now doing this:

	$tmp=imagecreatetruecolor($makewidth,$makeheight);	$color = imagecolorallocate($tmp,255,255,255);	imagefill($tmp,0,0,$color);

Link to comment
Share on other sites

If you want, I've got this function you might find useful:

function resize_image($opts){  $src = isset($opts['source']) ? $opts['source'] : '';  $dest = isset($opts['dest']) ? $opts['dest'] : '';  $w = isset($opts['w']) ? intval($opts['w']) : 0;  $h = isset($opts['h']) ? intval($opts['h']) : 0;    if ($src == '')  {    return;  }  if ($w == 0 && $h == 0)  {    return;  }  if ($dest == '')    $dest = $src; // resize in place  // open the image  $ext = strtolower(array_pop(explode('.', $src)));  switch ($ext)  {    case 'jpg':    case 'jpeg':      $i = imagecreatefromjpeg($src);    break;    case 'gif':      $i = imagecreatefromgif($src);    break;    case 'png':      $i = imagecreatefrompng($src);    break;    default:      return;  }  $new_w = imagesx($i);  $new_h = imagesy($i);  if (($w != 0 && $new_w < $w && $h == 0) ||      ($w == 0 && $h != 0 && $new_h < $h) ||      ($w != 0 && $new_w < $w && $h != 0 && $new_h < $h))  {    // image is small enough    if ($dest != $src)      copy($src, $dest);    return;  }  // determine new size  if ($w != 0 && $new_w > $w)  {    $new_h = ($w / $new_w) * $new_h;    $new_w = $w;  }  if ($h != 0 && $new_h > $h)  {    $new_w = ($h / $new_h) * $new_w;    $new_h = $h;  }    // resize  $new = imagecreatetruecolor($new_w, $new_h);  imagecopyresampled($new, $i, 0, 0, 0, 0, $new_w, $new_h, imagesx($i), imagesy($i));  imagedestroy($i);  // save the image  switch ($ext)  {    case 'jpg':    case 'jpeg':      imagejpeg($new, $dest);    break;    case 'gif':      imagegif($new, $dest);    break;    case 'png':      imagepng($new, $dest);    break;  }  imagedestroy($new);}

ex:

resize_image(array(  'source' => '/path/to/src.jpg',  'dest' => '/path/to/thumb.jpg', // optional, remove to overwrite source  'w' => 130,  'h' => 85));

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...