Page 1 of 1

resize images

Posted: Wed Oct 05, 2005 10:09 pm
by cbn4000
Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I am accepting images into my script and then resizing them to create both a feature image (400x300px) and a thumbnail (120x90px).  When creating the feature image, all goes well until an image comes in less than 400x300.  At this time, I just create the feature image with the original w and h.  This looks okay, but I would like the image to be 400x300.  So, my question is, is there a way to put a image that is, say 96x72, onto an image that is 400x300 without changing the image proportions?  In other words, the picture would remian the original size (96x72), but would be centered on an 400x300 image with a white background. 

Here is my current function:

Code: Select all

function createFeature($dirname,$img,$w,$h,$i)
{
   $imagedata = getimagesize($img);
   
   if($imagedata[0]>$w && $imagedata[1]>$h){
     if ($w && ($imagedata[0] < $imagedata[1])) 
     {
       $w = ($h / $imagedata[1]) * $imagedata[0];
     }
     else 
     {
       $h = ($w / $imagedata[0]) * $imagedata[1];
     }
   } else {
     $w=$imagedata[0];
     $h=$imagedata[1];
   }
   echo "<br>img = ".$img;
   echo "<br>w = ".$w;
   echo "<br>h = ".$h;
   echo "<br>imagedata[0] = ".$imagedata[0];
   echo "<br>imagedata[1] = ".$imagedata[1];
   

   $im2 = ImageCreateTrueColor($w,$h);
   $image = ImageCreateFromJpeg($img);
   imagecopyResampled ($im2, $image, 0, 0, 0, 0, $w, $h, $imagedata[0], $imagedata[1]);
   ImageJpeg($im2, "$dirname\pic.jpg", 70);
}

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Oct 06, 2005 10:05 am
by pickle
The easy part is to make a 400 X 300 image with a white background. You probably know how to do that so I won't bore you.

The slightly trickier part would be to center the image on that background. I think the formula to center an image would be:

Code: Select all

0.5($full_width) - 0.5($image_width) = target X coord
0.5($full_height) - 0.5($image_height) = target Y coord

so...

0.5(400) - 0.5(96) = 152
0.5(300) - 0.5(72) = 114
use imagecopyresampled() to copy the original image to the background.

Posted: Thu Oct 06, 2005 3:22 pm
by cbn4000
thanks Pickle!! it worked great!

Here is the modified code for anyone trying to do this:

Code: Select all

<?php
function createFeature($dirname,$img,$w,$h,$i)
{
   $dx=$dy=0;
   $imagedata = getimagesize($img);
   
   if($imagedata[0]>$w && $imagedata[1]>$h){
     if ($w && ($imagedata[0] < $imagedata[1])) 
     {
       $dw = ($h / $imagedata[1]) * $imagedata[0];
     }
     else 
     {
       $dh = ($w / $imagedata[0]) * $imagedata[1];
     }
   } else {
	 $dx=(0.5*$w)-(0.5*$imagedata[0]);
	 $dy=(0.5*$h)-(0.5*$imagedata[1]);
	 $dw=$imagedata[0];
	 $dh=$imagedata[1];

   }

   $im2 = ImageCreateTrueColor($w,$h);
   imagefill($im2, 0, 0, 0xFFFFFF);
   $image = ImageCreateFromJpeg($img);
   imagecopyResampled ($im2, $image, $dx, $dy, 0, 0, $dw, $dh, $imagedata[0], $imagedata[1]);
   ImageJpeg($im2, "$dirname\picture0.jpg", 70);
}	
?>

Posted: Thu Oct 06, 2005 3:26 pm
by marcscrookedfinger
Hey would you mind posting your working code? I'm in the need for a similar function. All of you code php tags and all would be great. thanks

Posted: Thu Oct 06, 2005 9:38 pm
by cbn4000
Here is the code that works correctly. I had to modify it a little more.

Code: Select all

function createFeature($dirname,$img,$w,$h)
{
   $dx=$dy=0;
   $imagedata = getimagesize($img);
   
   if($imagedata[0]>$w && $imagedata[1]>$h){
     if ($w && ($imagedata[0] < $imagedata[1])) 
     {
       $dw = ($h / $imagedata[1]) * $imagedata[0];
	   $dh=$h;
     }
     else 
     {
       $dh = ($w / $imagedata[0]) * $imagedata[1];
	   $dw=$w;
     }
   } else {
	 $dx=(0.5*$w)-(0.5*$imagedata[0]);
	 $dy=(0.5*$h)-(0.5*$imagedata[1]);
	 $dw=$imagedata[0];
	 $dh=$imagedata[1];

   }


   $im2 = ImageCreateTrueColor($w,$h);
   imagefill($im2, 0, 0, 0xFFFFFF);
   $image = ImageCreateFromJpeg($img);
   imagecopyResampled ($im2, $image, $dx, $dy, 0, 0, $dw, $dh, $imagedata[0], $imagedata[1]);
   ImageJpeg($im2, "$dirname\picture0.jpg", 70);
}
Just call this function with a directory path, the image path, and the width and height you want to make the new image to be.

So, a sample call might look like this:

Code: Select all

createFeature("mysite/images","c:\my_docs\images\vaction_pic.jpg",400,300);
Hope that helps
:D