Solved

Resizing an image on the fly??

Posted on 2006-06-23
5
496 Views
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:

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

      $page_title = 'Photos -- ' . $image;
      include('../nonPHPBBincludes/header.inc');

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


      <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>

<p>&nbsp;</p>


<?php
      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>';
?>
0
Comment
Question by:PurpleSlade
5 Comments
 
LVL 8

Assisted Solution

by:Autogard
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! (http://www.imagemagick.org/script/command-line-options.php)

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

Expert Comment

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

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

Bonmat86.
0
 
LVL 9

Accepted Solution

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

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

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

      }
else {
      $carpx=$width/$destWidth;
      $carpy=$height/$destHeight;
      if ($carpy>$carpx) $carp=$carpy;
      else $carp=$carpx;
      
      $new_Width=round($width/$carp);
      $new_Height=round($height/$carp);
      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);
      imagedestroy($image_p);
      imagedestroy($image);
      }
      }
0
 
LVL 1

Assisted Solution

by:dimakh
dimakh earned 200 total points
ID: 16975459
hi,

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;
     $src_img=imagecreatefromjpeg($name);

      $old_x=imagesx($src_img);
      $old_y=imagesy($src_img);
      
      $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);

        imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
        imagejpeg($dst_img,$filename);

      if($this->gdversion=="20"){
        $om = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled($om, $im, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);        
        }
        if($this->gdversion=="16"){
        $om = imagecreate($newWidth, $newHeight);
        imagecopyresized($om, $im, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);
        }
       
        return($om);

}

Hope the above solution works for you.

If any queries, please write me back.
0
 
LVL 2

Author Comment

by:PurpleSlade
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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…

803 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