Resizing an image on the fly??

Posted on 2006-06-23
Last Modified: 2008-02-01
Hi, I have a site where people can ftp photos to directories.  I have a php script when then traverses the directories and creates links to the photos.  The problem is that some of the files are way too big and on a screen set at a lower resolution, like 800 x 600 the pictures overlap some of  the css styles I have going.  My question is, can I process the files so that a smaller image is created on the fly?  This is my code that generates the image itself:

      $image = $_GET['img'];   // the image filepath is stored here

      $page_title = 'Photos -- ' . $image;

      echo '<body>';
      <div class="bluebottomsilver">
            <img border="0" src="../images/banner.jpg"><br />
      <img border="0" src="../images/banner4.png">

      <a href="../index.html">Home</a>
      <a href="mainimagepage.php">Photos</a>
      <a href="../index.php">Forums</a>
      <a href="../forms/MainRosterForm.php">Roster</a>


      echo "<img src='$image'>";  // this is where the image gets displayed.
      echo '<p>&nbsp;</p>';
      echo '<p>&nbsp;</p>';
      echo '</center>';
      echo '</body>';
      echo '</html>';
Question by:PurpleSlade
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

Assisted Solution

Autogard earned 100 total points
ID: 16974081
1. If you want to resize the image just for viewing you can just do: "<img src='$image' width='80%' height='60%'>".  You can also use pixels instead of percentages or you can use just width, or just height.

2. If you want to actually create an image that is smaller I would suggest running a system call to ImageMagick's "mogrify" command.  Something simple like "`mogrify -resize 50x50! image.jpg`" (the ` are backticks, the ! forces those dimensions otherwise it would scale keeping the ratio) should do the trick.  BUT BE CAREFUL -- make sure you have a backup of all of your images because mogrify will actually overwrite the original! (

3. PHP also has some image libraries that you can use (, but it's got an external dependency.  The imagecopyresized function may help.  There are also some examples on that page (search for resize).
LVL 10

Expert Comment

by:Khanh Doan
ID: 16974179
i think the best way is use
<img scr="image" with='80%' height="80%">
<img scr="image" with='200' height="200">

So you don't need to create a new resized-image.


Accepted Solution

tolgaong earned 200 total points
ID: 16974211
I have a function for it. It requires GD2 library.

imgResize($file,300,200); //it preserve the aspect ratio you can change

Function to add
function imgResize($filename,$width=150,$height=150,$compression=90){
if (file_exists($filename)){
$image = @imagecreatefromjpeg($filename) or die ("Cannot Initialize new GD image stream");
if ($srcPerc>$destPerc) {
      if ($carpy>$carpx) $carp=$carpy;
      else $carp=$carpx;
      if ($destWidth>$new_Width) $farkx=$destWidth-$new_Width;
      else $farkx=0;
      if ($destHeight>$new_Height) $farky=$destHeight-$new_Height;
      else $farky=0;      

else {
      if ($carpy>$carpx) $carp=$carpy;
      else $carp=$carpx;
      if ($destWidth>$new_Width) $farkx=$destWidth-$new_Width;
      else $farkx=0;
      if ($destHeight>$new_Height) $farky=$destHeight-$new_Height;
      else $farky=0;      
      $image_p = imagecreatetruecolor($new_Width, $new_Height);
      imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_Width, $new_Height, $width, $height);
      imagejpeg($image_p, $filename,$compression);

Assisted Solution

dimakh earned 200 total points
ID: 16975459

after going through your question, it seems you're trying to resize image while it gets loaded on the browser. i would not recommend this solution to go with.
just consider if your page hits are huge then your image resizing script is going to eat up a major memory resources of your server.

Instead I would suggest to create a cron schedular which will call your image resizing scripts at some fixed time.
so flow will be something like:
1. user uploads images to your server through FTP
2. your cron schedular runs at some fixed time (may be once or more) during the day. and calls image resing script to execute.
3. all the newly uploaded images gets resized and stored in different directory. lets say thumbnail diectory.
4. when users sees any image from your website, he/she will see only resized images i.e. stored in thumbail directory.

here is the code which you can use in image resizing script:
here i've written a function which uses GD Libraries for image manipulation. the code works on GD version 1.6 as well as 2.0
//variable definations as:
//$orig_file: absolute path of Orginal File uploaded,
//$dst_file: absolute path for the resized thumbnail to be saved,
//$new_w: width for thumbnail image,
//$new_h: height for thumbnail image

function createjpegthumb($orig_file,$dst_file,$new_w,$new_h){
    global $gd2;

      $maxAspect = $new_w / $new_h;
      $origAspect = $old_x / $old_y;

      if ($origAspect <= $maxAspect) {
            $thumb_w = $new_h * $origAspect;
            $thumb_h = $new_h;  
      } else {
            $thumb_w = $new_w;
            $thumb_h = $new_w / $origAspect;  
        $dst_img=imagecreatetruecolor($thumb_w, $thumb_h);


        $om = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled($om, $im, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);        
        $om = imagecreate($newWidth, $newHeight);
        imagecopyresized($om, $im, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);


Hope the above solution works for you.

If any queries, please write me back.

Author Comment

ID: 16977092
Thanks all - I think tolgaong and dimakh give me what I'm looking for which is a way to programatically create a new image, whether on the fly or scheduled somewhere.  In the meantime I went ahead and manually resized everything last night, but will use one of these in the future.  Thanks everyone.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

622 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