472,354 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

image resize

123 100+
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
22 4726
Atli
5,058 Expert 4TB
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
simon2x1
123 100+
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
8,658 Expert Mod 8TB
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
916 512MB
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
6,050 Expert 4TB
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
simon2x1
123 100+
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
1,168 Expert 1GB
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
6,050 Expert 4TB
@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
1,168 Expert 1GB
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
simon2x1
123 100+
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
6,050 Expert 4TB
@simon2x1
As I said above: it wouldn't make a difference.

Turn on PHP Debugging Messages
Feb 11 '09 #12
simon2x1
123 100+
is there any thing else i can do to make the image display
Feb 14 '09 #13
Markus
6,050 Expert 4TB
@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
simon2x1
123 100+
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
simon2x1
123 100+
How can I resize an image whenever the image is being uploaded directly to a folder?
Apr 3 '09 #16
Markus
6,050 Expert 4TB
@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
simon2x1
123 100+
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
1,584 Expert 1GB
@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
424 256MB
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
424 256MB
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
simon2x1
123 100+
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
simon2x1
123 100+
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

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

Similar topics

8
by: Chris Dewin | last post by:
Hi. I run a website for my band, and the other guys want an image gallery. I'm thinking it would be nice and easy, if we could just upload a jpg into a dir called "gallery/". When the client...
2
by: Carl Gilbert | last post by:
Hi I am looking for either a component or technique to allow me to do the following: * Provide a panel with a background image * Resize the image to best fit the panel to maintain aspect...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
1
by: Arjen | last post by:
Hi, I want to resize an image on my server. I tried a lot of samples... but all the time it does resize and saves the images but I can not view the image insize a webbrowser. With an...
2
by: Tim Arnold | last post by:
Hi, I'm using the Image module to resize PNG images from 300 to 100dpi for use in HTML pages, but I'm losing some vertical and horizontal lines in the images (usually images of x-y plots). ...
2
by: Dominic Vella | last post by:
Hi, I know I seem to have the really complicated questions, but I guess that's why I'm here. This is a little verbose, only because I've been trying to crack this for a week now. Your help...
3
by: danielatdaveschool | last post by:
Hi, noob here Im using mod_python and apache2 using psp for output of page, i open a file and resize it with the following code <% import Image, util fields = util.FieldStorage(req)...
8
by: infoseekar | last post by:
Image Resize & Rotation Hi I have 2 scripts, one for Image rotation and other image resize and they both are working. Image resize scripts load the picture and resize it and Image rotation...
14
anfetienne
by: anfetienne | last post by:
hi.....i have this script (below #1) that is linked to another php file SimpleImage.php (#2) im trying to get it to work on my uploaded images but it keeps coming up errors.....i haven't altered...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.