Welcome to the Linux community forum and interact with 2 million technical staff. What surprised us when we enter PHP is that we need to worry about ASP in image processing. Using the GD library PHP, we can easily implement thumbnails. The purpose of this article is to use GD to generate a thumbnail. PHP can generate a thumbnail directly to the browser or store it as a file.
Welcome to the Linux community forum and interact with 2 million technical staff> what surprised us when we enter PHP is that ASP is a concern in image processing, using the GD library PHP, you can easily implement thumbnails. The purpose of this article is to use GD to generate a thumbnail. PHP can generate a thumbnail directly to the browser or store it as a file.
Welcome to the Linux community forum and interact with 2 million technicians>
What surprised us with PHP is that we need to worry about ASP in image processing. Using the GD library PHP, we can easily implement thumbnails. The purpose of this article is to use GD to generate thumbnails. PHP can directly generate the thumbnails and deliver them to the browser. It can also store them in the form of files to the hard disk.
In the process of generating thumbnails, we need to use several functions in the GD library:
Getimagesize (string filename [, array var]) to obtain the image information. The returned value is a one-person array, which includes several pieces of information $ var [0] ---- return the width of the image, $ var [1] ---- return height, [2] Return the type of the image file, [4] returns the width = "", height = "" information.
ImageX (resource image)
ImageY (resource image) returns the width and height of the image
Imagecopyresized (des img, src img, int des_x, int des_y, int src_x, int src_y, int des_w, int des_h, int src_w, int src_y) copies and captures regional images.
Imagecreatetruecolor (int width, int height) to create a true color chart
Imagejpeg (resource image)
The following Code is used:
# Constants
Define (IMAGE_BASE, '/var/www/html/mbailey/images ');
Define (MAX_WIDTH, 150 );
Define (MAX_HEIGHT, 150 );
# Get image location
$ Image_file = str_replace ('','', $ _ SERVER ['query _ string']);
$ Image_path = IMAGE_BASE. "/$ image_file ";
# Load image
$ Img = null;
$ Ext = strtolower (end (explode ('.', $ image_path )));
If ($ ext = 'jpg '| $ ext = 'jpeg '){
$ Img = @ imagecreatefromjpeg ($ image_path );
} Else if ($ ext = 'png '){
$ Img = @ imagecreatefrompng ($ image_path );
# Only if your version of GD except des GIF support
} Else if ($ ext = 'gif '){
$ Img = @ imagecreatefrompng ($ image_path );
}
# If an image was successfully loaded, test the image for size
If ($ img ){
# Get image size and scale ratio
$ Width = imagesx ($ img );
$ Height = imagesy ($ img );
$ Scale = min (MAX_WIDTH/$ width, MAX_HEIGHT/$ height );
# If the image is larger than the max shrink it
If ($ scale <1 ){
$ New_width = floor ($ scale * $ width );
$ New_height = floor ($ scale * $ height );
# Create a new temporary image
$ Tmp_img = imagecreatetruecolor ($ new_width, $ new_height );
# Copy and resize old image into new image
Imagecopyresized ($ tmp_img, $ img, 0, 0, 0, 0,
$ New_width, $ new_height, $ width, $ height );
Imagedestroy ($ img );
$ Img = $ tmp_img;
}
}
# Create error image if necessary
If (! $ Img ){
$ Img = imagecreate (MAX_WIDTH, MAX_HEIGHT );
Imagecolorallocate ($ img, 0, 0 );
$ C = imagecolorallocate ($ img, 70, 70 );
Imageline ($ img, 0, 0, MAX_WIDTH, MAX_HEIGHT, $ c2 );
Imageline ($ img, MAX_WIDTH, 0, 0, MAX_HEIGHT, $ c2 );
}
# Display the image
Header ("Content-type: image/jpeg ");
Imagejpeg ($ img );
?>
[1] [2]