iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 11, 2008
Improving accessibility for motor impaired users
WebDevTips UK
 
May 11, 2008
10 ways to orientate users on your site
WebDevTips UK
 
May 11, 2008
Web Design Clinic - Rros restoration camp 2006
About
 
May 10, 2008
The Moods of Facebook
About
 
May 9, 2008
CSS 1 properties are a great start
About
 
May 9, 2008
Reader Question: How do you get fancy fonts?
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

Making thumbnails with php 

  Views:    6913
  Votes:    6
by Diego Botello 12/12/03 Rating: 

Synopsis:

Article explaining how to make thumbnails with php
Pages: 
The Article

 

Php makes it easy to create thumbnails if you have the GD library

installed. Lets see the following example.

 

<?

header("Content-type: image/jpeg");

$first=ImageCreateFromJPEG("myimage.jpg") or die("couldn't open image");

$second=ImageCreate(100,150) or die("couldn't create image");

ImageCopyResized($second,$first,0,0,0,0,100,150,imagesx($first),imagesy($first)) or die("coudln't resize image");

ImageJPEG($second,"thumb.jpg") or die("couldn't save image");

?>

 

In the example above we will create a thumbnail of 100x150 pixels

named thumb.jpg from the image myimage.jpg. Lets see the code step by step.

 

header("Content-type: image/jpeg");

 

tells the browser that the following content is an image of type jpeg.

If you don't provide a header the browser will work with the data as

if it where an html file.

 

$first=ImageCreateFromJPEG("myimage.jpg") or die("couldn't open image");

 

The imagecreatefromjpeg function is used to open an existing image,

myimage.jpg (if you want to work with png files you should use

imagecreatefrompng instead). This function returns an image identifier

that is stored in the $first variable.

 

$second=ImageCreate(100,150) or die("couldn't create image");

 

the function imagecrate is used to create a new image of size 100x150.

The variable $second stores the image identifier.

 

ImageCopyResized($second,$first,0,0,0,0,100,150,ImageSX($first),ImageSY($first)) or die("couldn't resize image");

 

Imagecopyresized is the function used to create a resized copy of

our image. To use it we must provide the identifier of the image we

want to create ($second); the identifier of the first image ($first);

the following zeros indicate that we want to copy all the image(they

are the x and y coordinates of upper left corner for the destination

image and the source image respectively); 100,150 indicates that we

want a new image of that size in pixels; and imagesx($first),

imagesy($first) indicate the width and height of the source image

(imagesx and imagesy are image functions that return the width and

height respectively).

 

ImageJPEG($second,"thumb.jpg") or die("couldn't save image");

 

Finally, imagejpeg saves the file which identifier is $second, and saves

it with the name thumb.jpg. Notice that if you want to use png images

you should use imagepng instead.

Pages: 

Similar/related articles:


 
  Sponsors