By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

image resize

100+
P: 123
i have an image which width is 213 and height is 200 when
i echo the image and i resize it
echo "<img src='company/$present' width='70' height='68'/>";
the image was not as clear as when it was 213 * 200.how can i
make the image clear after i have resize it to 70 * 200.
Jan 23 '09 #1
Share this Question
Share on Google+
22 Replies


Atli
Expert 5K+
P: 5,058
Hi.

When you resize it as you do, with the <img> height and width attributes, you are essentially leaving it up to the browser to resize it.
Predictably, Internet Explorer does a horrible job at this, while the other major browsers do a pretty good job resizing them.

If you want to ensure that the image is scaled smoothly, you can have PHP resize the image. The GD extension offers the imagecopyresampled function, which does an excellent job resizing images. (See the documentation for examples on how to use it)

Note that image processing is a very resource intensive task, so if you frequently display you images resized, you should save the resized image to the server's file-system and serve it from there, rather then resize it for every request.
Jan 23 '09 #2

100+
P: 123
what if i want to echo the image from my database, which
will involve vaious image how can i then resize it and
also keep the resize image smooth like the original.i
will appreciate it if the answer that will be given to me will
be edited on my code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $query = "SELECT * FROM photo";
  3. $result = mysql_query ($query) or die('query error');
  4. $count = 0;
  5. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
  6. $image = $line[picname];
  7. echo "<img src='company/$image'/> ";
  8. $count++;
  9. }
  10.  
Jan 26 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
if you want to echo the image from DB (that is, you have the image itself stored there (as binary data)) you need a script of its own that fetches the image, applies any changes you want (e.g. resizing) and gives out the result.
Jan 26 '09 #4

tharden3
100+
P: 916
This article about Uploading files into a MySQL database using PHP
might help you. It discusses a great way to handle image files, although it might not directly answer your "resizing" question. Just throwing it out there as a reference.

Thanks Atli ;)
Jan 26 '09 #5

Markus
Expert 5K+
P: 6,050
Resizing images 'on the fly' (whenever you pull them from a database, etc) will eat into your resources like a fat kid into some delicious cake. When a photo is uploaded, use the GD extension that Atli linked to, and then save a seperate copy of the resized image. That way, you're saving resources, but sacrificing a small amount of HDD space.
Jan 26 '09 #6

100+
P: 123
i have a problem with the following code below, the image is not showing
the image is display as if there is a missing link, i have check the code
and i see not thing wrong with it and aslo i have enable the GD Library on
my wamp server

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //my Image
  3. $imgSrc = '36.jpg'; 
  4. list($width, $height) = getimagesize($imgSrc); 
  5. //saving the image into memory (for manipulation with GD Library)
  6. $myImage = imagecreatefromjpeg($imgSrc); 
  7. if($width > $height) $biggestSide = $width; 
  8. else $biggestSide = $height; 
  9. $cropPercent = .5; 
  10. $cropWidth   = $biggestSide*$cropPercent; 
  11. $cropHeight  = $biggestSide*$cropPercent; 
  12. $c1 = array("x"=>($width-$cropWidth)/2, "y"=>($height-$cropHeight)/2);
  13. $thumbSize = 60; 
  14. $thumb = imagecreatetruecolor($thumbSize, $thumbSize); 
  15. imagecopyresampled($thumb, $myImage, 0, 0, $c1['x'], $c1['y'], $thumbSize, $thumbSize, $cropWidth, $cropHeight); 
  16. $lineWidth = 1;
  17. $margin    = 0;  
  18. $green    = imagecolorallocate($thumb, 193, 252, 182);
  19.  
  20. for($i=0; $i<2; $i++){
  21. imagefilledrectangle($thumb, $margin, $margin, $margin+$lineWidth, $thumbSize-$margin, $green); 
  22. imagefilledrectangle($thumb, $thumbSize-$margin-$lineWidth, $margin, $thumbSize-$margin, $thumbSize-$margin, $green);
  23. imagefilledrectangle($thumb, $margin, $margin, $thumbSize-$margin-$lineWidth, $margin+$lineWidth, $green); 
  24. imagefilledrectangle($thumb, $margin, $thumbSize-$margin-$lineWidth, $thumbSize-$margin-$lineWidth, $thumbSize-$margin,$green);
  25. $margin+=4; 
  26.     header('Content-type: image/jpeg');
  27.     imagejpeg($thumb);
  28.     imagedestroy($thumb);
  29.  
  30. ?>
Feb 7 '09 #7

TheServant
Expert 100+
P: 1,168
Line 7, 8 should have {curly brackets}:
Expand|Select|Wrap|Line Numbers
  1. if($width > $height) {$biggestSide = $width;}
  2. else {$biggestSide = $height;}
Other than that I can't see anything big... Will look again later if noone has replied.
Feb 8 '09 #8

Markus
Expert 5K+
P: 6,050
@TheServant
Not true in this case; because the conditional statement only has one statement within it, curly brackets can be omitted.

Expand|Select|Wrap|Line Numbers
  1. // will work
  2. if ( $a === $b )
  3.  echo "hi";
  4. else
  5.  echo "yo";
  6.  
  7. // won't work
  8. if ( $a === $b )
  9.  echo "hello ";
  10.  echo "world";
  11. else
  12.  echo "hi";
  13.  
Feb 8 '09 #9

TheServant
Expert 100+
P: 1,168
Interesting. I had a feeling that was the case, but wasn't 100% sure. Anyway, I use them all the time for consistency. Thanks for the tip. Suggestion withdrawn.
Feb 9 '09 #10

100+
P: 123
i have made the neccessary adjustment to line 7 and 8 of the code... but the image is still not displaying as told.
Feb 11 '09 #11

Markus
Expert 5K+
P: 6,050
@simon2x1
As I said above: it wouldn't make a difference.

Turn on PHP Debugging Messages
Feb 11 '09 #12

100+
P: 123
is there any thing else i can do to make the image display
Feb 14 '09 #13

Markus
Expert 5K+
P: 6,050
@simon2x1
Have you turned on debugging messages? What does happen? What doesn't happen.

The more info you give us, the better.
Feb 14 '09 #14

100+
P: 123
i have visited the link (imagecopyresample) you gave to me and i try almost
every code on the link non gave me an error but the image still did not display
like the code below is met to resize an image but the image did not display.
i have turn on my Gd library on my wamp server, the image i want to resize is
in the directory with the page so what is wrong and what should i do
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // define image to resize
  3. $file = "Aizen.jpg";
  4. // create object from original image
  5. $imOrig = imagecreatefromjpeg($file);
  6. list($width, $height) = getimagesize($file);
  7. $imNew = imagecreatetruecolor($width * 1.5, $height * 1.5);
  8. // resample and resize old image
  9. imagecopyresampled($imNew, $imOrig, 0, 0, 0, 0, $width * 1.5,
  10. $height * 1.5, $width, $height);
  11. // output resized image
  12. header("Content-type: image/jpeg");
  13. imagejpeg($imNew);
  14. imagedestroy($im);
  15. ?>
  16.  
Mar 7 '09 #15

100+
P: 123
How can I resize an image whenever the image is being uploaded directly to a folder?
Apr 3 '09 #16

Markus
Expert 5K+
P: 6,050
@simon2x1
What do you mean by 'uploaded directly to a folder'? Is a PHP script handling the upload? Or are we speaking of FTP, or whatever?

Don't be lazy when asking a question.

- mark.
Apr 3 '09 #17

100+
P: 123
sorry i mean a php script handling the upload that automatically resize an image we a person upload an image
Apr 5 '09 #18

dlite922
Expert 100+
P: 1,584
@simon2x1
To do anything with an image, it must first be wholly downloaded to the server. After that PHP (or any other program on the server) can manipulated.

Checkout GD, Imagick() libraries at php.net. After you've got access to these image libraries resizing will be trivial. After resize you can save it anywhere PHP/Apache user has permission to on the filesystem.





Dan
Apr 7 '09 #19

anfetienne
100+
P: 424
save this code as SimpleImage.php

Expand|Select|Wrap|Line Numbers
  1. <title>: : : members online resource centre</title><?php
  2. /*
  3. * File: SimpleImage.php
  4. * Author: Simon Jarvis
  5. * Copyright: 2006 Simon Jarvis
  6. * Date: 08/11/06
  7. * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  8. * This program is free software; you can redistribute it and/or 
  9. * modify it under the terms of the GNU General Public License 
  10. * as published by the Free Software Foundation; either version 2 
  11. * of the License, or (at your option) any later version.
  12. * This program is distributed in the hope that it will be useful, 
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  15. * GNU General Public License for more details: 
  16. * http://www.gnu.org/licenses/gpl.html
  17. *
  18. */
  19.  
  20. class SimpleImage {
  21.  
  22.    var $image;
  23.    var $image_type;
  24.  
  25.    function load($filename) {
  26.       $image_info = getimagesize($filename);
  27.       $this->image_type = $image_info[2];
  28.       if( $this->image_type == IMAGETYPE_JPEG ) {
  29.          $this->image = imagecreatefromjpeg($filename);
  30.       } elseif( $this->image_type == IMAGETYPE_GIF ) {
  31.          $this->image = imagecreatefromgif($filename);
  32.       } elseif( $this->image_type == IMAGETYPE_PNG ) {
  33.          $this->image = imagecreatefrompng($filename);
  34.       }
  35.    }
  36.    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  37.       if( $image_type == IMAGETYPE_JPEG ) {
  38.          imagejpeg($this->image,$filename,$compression);
  39.       } elseif( $image_type == IMAGETYPE_GIF ) {
  40.          imagegif($this->image,$filename);         
  41.       } elseif( $image_type == IMAGETYPE_PNG ) {
  42.          imagepng($this->image,$filename);
  43.       }   
  44.       if( $permissions != null) {
  45.          chmod($filename,$permissions);
  46.       }
  47.    }
  48.    function output($image_type=IMAGETYPE_JPEG) {
  49.       if( $image_type == IMAGETYPE_JPEG ) {
  50.          imagejpeg($this->image);
  51.       } elseif( $image_type == IMAGETYPE_GIF ) {
  52.          imagegif($this->image);         
  53.       } elseif( $image_type == IMAGETYPE_PNG ) {
  54.          imagepng($this->image);
  55.       }   
  56.    }
  57.    function getWidth() {
  58.       return imagesx($this->image);
  59.    }
  60.    function getHeight() {
  61.       return imagesy($this->image);
  62.    }
  63.    function resizeToHeight($height) {
  64.       $ratio = $height / $this->getHeight();
  65.       $width = $this->getWidth() * $ratio;
  66.       $this->resize($width,$height);
  67.    }
  68.    function resizeToWidth($width) {
  69.       $ratio = $width / $this->getWidth();
  70.       $height = $this->getheight() * $ratio;
  71.       $this->resize($width,$height);
  72.    }
  73.    function scale($scale) {
  74.       $width = $this->getWidth() * $scale/100;
  75.       $height = $this->getheight() * $scale/100; 
  76.       $this->resize($width,$height);
  77.    }
  78.    function resize($width,$height) {
  79.       $new_image = imagecreatetruecolor($width, $height);
  80.       imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  81.       $this->image = $new_image;   
  82.    }      
  83. }
  84. ?>
  85.  
  86.  
  87.  
  88.  
use this code to help you upload/resize the images

Expand|Select|Wrap|Line Numbers
  1. include('SimpleImage.php');
  2. while(list($key,$value) = each($_FILES['images']['name']))
  3.         {
  4.             if(!empty($value))
  5.             {
  6.                 $filename = $value;
  7.                     $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
  8.  
  9.                     $add = "your/directory/$filename";
  10.                        //echo $_FILES['images']['type'][$key];
  11.                  // echo "<br>";
  12.                     copy($_FILES['images']['tmp_name'][$key], $add);
  13.                     chmod("$add",0777);
  14.  
  15.             }
  16. $image = new SimpleImage();
  17. $image->load("$add");
  18. $image->resize(546,403);
  19. $image->save("$add"); 
  20.  
  21.         }
  22.  
to change the size you wish the images to be you edit this part of the above code

$image = new SimpleImage();
$image->load("$add");
$image->resize(546,403);
$image->save("$add");

the 1st digit (546) being the width and the 2nd digit (403) being the height.....change them as needed.

make sure that the name values on your upload form is set for an array.....name all fields images[].....see example form below

Expand|Select|Wrap|Line Numbers
  1.     <form name="the_form" id="the_form" method="POST" action="yourfile.php" enctype="multipart/form-data">
  2.                 1. <input name="images[]" type="file" class="imgfield" id="field_1" 
  3.                 2. <input name="images[]" type="file" class="imgfield" id="field_2" 
  4.                 3. <input name="images[]" type="file" class="imgfield" id="field_3" 
  5.                 4. <input name="images[]" type="file" class="imgfield" id="field_4" 
  6.                 5. <input name="images[]" type="file" class="imgfield" id="field_5" 
  7.           <input name="count" type="hidden" id="count" value="4"/>
  8.  
  9. <input name="Submit" type="submit" value="Upload Image" />
  10. </form>
  11.  
hope this helps
Apr 7 '09 #20

anfetienne
100+
P: 424
also you dont need to change any part of that coding only the size you want the image to be and the names on your forms
Apr 7 '09 #21

100+
P: 123
the code worked, it reduced the image which i uploaded thanks for that but when i clicked submit without uploading the image it gave me error that look
like unreadable word so i put the folowing if($_FILES['images']['size'] == 0)
between line 4 and 5 it gave me the same error put it between line 14 and 15 the same error please i dont want the upload feild to post a blank field and also not to allow any image which is not gif, jpeg and png how can you help i need this for my project
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  include('SimpleImage.php');
  3.  if (isset($_POST['submit'])) {
  4.  while(list($key,$value) = each($_FILES['images']['name'])){
  5.              if(!empty($value))
  6.              {
  7.                  $filename = $value;
  8.                      $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
  9.             $add = "./collect/$filename";
  10.                         echo $_FILES['images']['type'][$key];
  11.                   echo "<br>";
  12.                      copy($_FILES['images']['tmp_name'][$key], $add);
  13.                      chmod("$add",0777);
  14.         }
  15.  $image = new SimpleImage();
  16.  $image->load("$add");
  17.  $image->resize(100,100);
  18.  $image->save("$add"); 
  19.  
  20.          } 
  21.         }
  22.          ?>
  23.       <form name="the_form" id="the_form" method="POST" action="home.php" enctype="multipart/form-data">
  24.        <input name="images[]" type="file"  id="field_1" > 
  25.      <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
  26.  <input name="submit" type="submit" value="submit" />
  27.  </form>
  28.  
May 16 '09 #22

100+
P: 123
the code worked, it reduced the image which i uploaded thanks for that but when i clicked submit without uploading the image it gave me error that look
like unreadable word so i put the folowing if($_FILES['images']['size'] == 0)
between line 4 and 5 it gave me the same error put it between line 14 and 15 the same error please i dont want the upload feild to post a blank field and also not to allow any image which is not gif, jpeg and png how can you help i need this for my project

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  include('SimpleImage.php'); 
  3.  if (isset($_POST['submit'])) { 
  4.  while(list($key,$value) = each($_FILES['images']['name'])){ 
  5.              if(!empty($value)) 
  6.              { 
  7.                  $filename = $value; 
  8.                      $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line 
  9.             $add = "./collect/$filename"; 
  10.                         echo $_FILES['images']['type'][$key]; 
  11.                   echo "<br>"; 
  12.                      copy($_FILES['images']['tmp_name'][$key], $add); 
  13.                      chmod("$add",0777); 
  14.         } 
  15.  $image = new SimpleImage(); 
  16.  $image->load("$add"); 
  17.  $image->resize(100,100); 
  18.  $image->save("$add");  
  19.  
  20.          }  
  21.         } 
  22.          ?> 
  23.       <form name="the_form" id="the_form" method="POST" action="home.php" enctype="multipart/form-data"> 
  24.        <input name="images[]" type="file"  id="field_1" >  
  25.      <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> 
  26.  <input name="submit" type="submit" value="submit" /> 
  27.  </form> 
  28.  
  29.  
May 26 '09 #23

Post your reply

Sign in to post your reply or Sign up for a free account.