Solved

Image resize / autofit

Posted on 2002-03-18
3
328 Views
Last Modified: 2012-06-27
Hi, thanks to the help of Experts, I have managed to make a util to allow users on my site to browse to an image on their disks and upload it to my server and automatically display it on their pages,
Now, I want to go one step further, to automatically resize / autofit to the best dimensions in the space that I provide.

I have seen comments about imagemagic, but couldn't find examp0les simple enough for me to understand. Could someone here pleaae try to give me an example of how to do this.
It doesn't HAVE to be with using imagemagic, a simple algorith reading the width and height of image at upload time adn then proportionally resizing the image to fit within a pre-set (x,y) will do also.

Thanks
0
Comment
Question by:psimation
  • 2
3 Comments
 

Accepted Solution

by:
icewolf_li earned 50 total points
ID: 6878725
At first you should install GD library for php, then u can use gd functions as follow:

/*
rend a new $dstFile from $srcFile,with the max size limit $maxsize.
*/
function RendSmall($srcFile,$dstFile,$maxsize) {
     
     $data = GetImageSize($srcFile,&$info);
     switch ($data[2]) {
          case 1:
          $im = @ImageCreateFromGIF($srcFile);
          break;
          case 2:
          $im = @imagecreatefromjpeg($srcFile);
          break;
          case 3:
          $im = @ImageCreateFromPNG($srcFile);
          break;
          default:
               return -1;
     }
     if(!$im)
          return -1;

     $dstW = $data[0];
     $dstH = $data[1];
     $dstX = 0;
     $dstY = 0;
     if($data[0]>=$data[1]){
          if($data[0] > $maxsize){
               $dstW = $maxsize;
               $dstH = $maxsize * $data[1] / $data[0];
               $dstY = ($maxsize-$dstH)/2;
          }
          else{
               $maxsize = $data[0];
               $dstY = ($data[0]-$dstH)/2;
          }
     }
     else{
          if($data[1] > $maxsize){
               $dstW = $maxsize * $data[0] / $data[1];
               $dstH = $maxsize;
               $dstX = ($maxsize-$dstW)/2;

          }
          else{
               $maxsize = $data[1];
               $dstX = ($data[1]-$dstH)/2;
          }
     }
     $srcW=ImageSX($im);
     $srcH=ImageSY($im);
     $ni=ImageCreate($maxsize,$maxsize);
     
     if(!$ni || !(ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$dstW,$dstH,$srcW,$srcH)) )
          return -1;
     if(!ImageJpeg($ni,$dstFile))
          return -1;
     
     return 1;
}
0
 
LVL 17

Author Comment

by:psimation
ID: 7135371
Sorry for the delay.
Can you help me to get GD installed first? I tried, but cannot get it right.
HAve RH7.0 with PHP that seems to have been installed with the --without-gd switch, I don't want to re-istall the entire PHP, so if you have  a solution to load it at runtime, it would be much better solution.

Thanks
0
 
LVL 17

Author Comment

by:psimation
ID: 7219793
OK, this question didn't help me as I couldn't get GD installed, but I guess, for others out there, it might help, so there are merrit in having this question in the archives...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now