resize images

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
cbn4000
Forum Newbie
Posts: 3
Joined: Wed Oct 05, 2005 9:54 pm

resize images

Post 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]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
cbn4000
Forum Newbie
Posts: 3
Joined: Wed Oct 05, 2005 9:54 pm

Post 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);
}	
?>
marcscrookedfinger
Forum Newbie
Posts: 2
Joined: Thu Oct 06, 2005 3:15 pm

Post 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
cbn4000
Forum Newbie
Posts: 3
Joined: Wed Oct 05, 2005 9:54 pm

Post 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
Post Reply