Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

imagecopyresampled - change height relative to width

Status
Not open for further replies.
Aug 23, 2004
174
US
I have CMS and I am trying to have uploaded images width shrink to 228px and shrink the height relative to that width.

Here is the current code I am using. I have a set width and the code works, but I need to get rid of the height and have it change dynamicaly.
Code:
   $newwidth = 228;
   $newheight = 163;

   $cur_dir = "../uploads/products/";
   
   $filename = $cur_dir.$cur_file;
   if(preg_match("/.jpg/i", "$filename"))
   {
       $format = 'image/jpeg';
   }
   if (preg_match("/.gif/i", "$filename"))
   {
       $format = 'image/gif';
   }
   if(preg_match("/.png/i", "$filename"))
   {
       $format = 'image/png';
   }
   if($format!='')
   {
       list($width, $height) = getimagesize($filename);
       switch($format)
       {
           case 'image/jpeg':
           $source = imagecreatefromjpeg($filename);
           break;
           case 'image/gif';
           $source = imagecreatefromgif($filename);
           break;
           case 'image/png':
           $source = imagecreatefrompng($filename);
           break;
       }
       $thumb = imagecreatetruecolor($newwidth,$newheight);
       imagealphablending($thumb, false);
       $source = @imagecreatefromjpeg("$filename");
       imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
       $filename="$output_dir/$cur_file";
       @imagejpeg($thumb, $filename);

Can anyone help me out?

Thanks
 
You are going to have to use the height and width you get from the getimagesize() function to calculate the new dimensions.

Something like this placed after you get the values from the getimagesize() function should work:

Code:
$newheight=round($height/($width/228));

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top