Solved

Resizing an image on the fly??

Posted on 2006-06-23
5
492 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

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

Suggested Solutions

Title # Comments Views Activity
php email 2 26
what is best version of php to use 6 46
Php pie charts 3 26
Change text to radio button and calendar form 2 34
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

920 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

16 Experts available now in Live!

Get 1:1 Help Now