Jump to content

Problem: Image Resize Script enlarges small pictures to fit forced height &width


nevynev

Recommended Posts

Hi there,

 

If I have an image which has a height/witdth smaller than the standard $forcedwitdht and $forcedheight and use the function below it resizes them to fit that size but I want it to remain the original size.

 

For clarity.

 

if $forcedheight is 400

and $forcedwidth is 500

 

If I use this function on a 50 x 70 image I DONT want it to be resized to 400x500. Obvisouly at the moment the script is aimed at larger images (e.g. 1024 x 768).

 

The function is:

 

<?php
function resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $destfile, $imgcomp)
   {
   $g_imgcomp=100-$imgcomp;
   $g_srcfile=$sourcefile;
   $g_dstfile=$destfile;
   $g_fw=$forcedwidth;
   $g_fh=$forcedheight;

   if(file_exists($g_srcfile))
       {
       $g_is=getimagesize($g_srcfile);
       if(($g_is[0]-$g_fw)>=($g_is[1]-$g_fh))
           {
           $g_iw=$g_fw;
           $g_ih=($g_fw/$g_is[0])*$g_is[1];
           }
           else
           {
           $g_ih=$g_fh;
           $g_iw=($g_ih/$g_is[1])*$g_is[0];    
           }
       $img_src=imagecreatefromjpeg($g_srcfile);
       $img_dst=imagecreatetruecolor($g_iw,$g_ih);
       imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]);
       imagejpeg($img_dst, $g_dstfile, $g_imgcomp);
       imagedestroy($img_dst);
       return true;
       }
       else
       return false;
   } 

?>

 

I guess it would be a simple if (height and width is less than){}else{} sort of thing but I can't seem to make it work.

 

Thanks for any help

NevyNev

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.