Manual of PHP

 
 
 

Web map

 
rim   rim
arrowReady frontarrow of Codes PHParrow Like generating a Thumbnail in PHP using GD

 

Like generating a Thumbnail in PHP using GD To print E-mail

 

First of all to warn that the bookstores GD are needed to be able to realize the thumbnails. And according to the GD version, we will be able to use one or another function

First I will give the code of a function with which we will generate straight a thumbnail of high quality.

function thumbjpeg ($imagen, $altura) {

//Place where the thumbnails will keep with regard to the folder where the "big" image is.
$dir_thumb = "thumbs /";

//Prefix that will be added to the name of the thumbnail. Example: if the big image was "imagen1.jpg",
//the thumbnail would call "tn_imagen1.jpg"
$prefijo_thumb = "tn _";
$camino_nombre=explode ("/", $imagen);

//Here we will have the name of the image.
$nombre=end ($camino_nombre);

//Here the route specified to look for the image.
$camino=substr ($imagen, 0, strlen ($imagen)-strlen ($nombre));

//We try to create the thumbnails directory, if it did not exist previously.
if (! file_exists ($camino.$dir_thumb))
mkdir ($camino.$dir_thumb, 0777) or die ("could not have created the directory $dir_thumb");

//Here comprovamos that the image that we want to create does not exist previously
if (! file_exists ($camino.$dir_thumb.$prefijo_thumb.$nombre)) {
I begin $camino.$dir_thumb.$prefijo_thumb.$nombre." It did not exist <br> n";
$img = imagecreatefromjpeg ($camino.$nombre) or die ("does not find the image $camino$nombre <br> n");

//we look at the size of the original image...
$datos = getimagesize ($camino.$nombre) or die ("Problems with $camino$nombre <br> n");

//we try to climb the original image to the measurement that we are interested in
$ratio = ($datos [1] / $altura);
$anchura = round ($datos [0] / $ratio);

//this will be the new re-climbed image
$thumb = imagecreatetruecolor ($anchura, $altura);

//with this function we re-climb it
imagecopyresampled ($thumb, $img, 0, 0, 0, 0, $anchura, $altura, $datos [0], $datos [1]);

//voilà we save it with the name and in the place that we are interested in.
imagejpeg ($thumb,$camino.$dir_thumb.$prefijo_thumb.$nombre);
}
}


To call to the function simply to do:

thumbjpeg ($imagen, 125);

In this case, '$imagen', it is the image that we want to reduce, and '125', it is the height in pixels that we want that it has the limited image, so that the breadth remains provided with regard to the original image.

With the instruction “or die ()” will show herself on screen the message entrecomillado alone in case it was trumping the first judgment of the line.

NOTES:

If instead of GD2 we have of GD1, we will not be able to use the function imagecopyresampled () and we should be content with the function imagecopyresized (), that uses the same parameters, the bad thing is that the last one gives like turned out an image of poorer quality. In return, it is much more rapid than the previous one.

This way if we have GD2, we can use the one that more we are interested in, if we want QUALITY and there does not matter for us the number of cycles consumed in the servant,

imagecopyresampled ();

in the opposite case:

imagecopyresized ();

If what we want is to create images "to the flight", without keeping them in any file (slightly advisable thing if we use the function of more quality), the second parameter of the function imagejpeg () must not put itself, staying the call in the scritp that exists more above:

imagejpeg ($thumb);

Very well, démonos it tells that this alone script serves to reduce images of type JPEG, not of other, for other formats, it would be necessary only to change a pair of functions into the correspondents into the formats that we are interested in.

Example:

imagecreatefromjpeg () for imagecreatefrompng () or imagecreatefromwbmp () or etc.

and

imagejpeg () for imagepng () or imagewbmp () or etc.

Author: Basilio Vera.
http://www.aclantis.com/section-viewarticle-145.html

 

 
Front
Chapters of the Manual of PHP
Introduction to PHP
Variables
My First Script PHP
Operators in PHP
Structures of Control
Functions in PHP
Code inclusion
Counterfoils (array)
Chains of Characters
Classes
Dates
Entry and Exit
Operations with Files
The language SQL and PHP
Connection with MySQL
Meetings
Forms with PHP
To practise in line
List of practices in line
FAQ
Frequent questions
Codes PHP
List of Codes PHP
Forums
Forums PHP
Other Manuals
Manuals of other languages
 
   
 
 
MySpaceScripts.info - Sitemap - Contact us - Sitemap - Contact us - Sitemap - Contact us

kasa fiskalna , hollywood gossips , buy-usedcars.co.uk , DMC Poland , food supplements , bílé víno , speed test