Solved

Image resize / autofit

Posted on 2002-03-18
3
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

724 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