PHP code for changing the image size _ php skills

Source: Internet
Author: User
Tags imagejpeg
Changing the size of an image is a common functional requirement. next we will study how to change the size of an image in PHP. First, we will introduce a self-written function.

The code is as follows:


$ Imgsrc = "http://www.nowamagic.net/images/3.jpg ";
$ Width = 780;
$ Height = 420;
Resizejpg ($ imgsrc, $ imgdst, $ width, $ height );
Function resizejpg ($ imgsrc, $ imgdst, $ imgwidth, $ imgheight)
{
// $ Imgsrc jpg format image path $ imgdst jpg format Image save file name $ imgwidth to change the width $ imgheight to change the height
// Obtain the image width and height
$ Arr = getimagesize ($ imgsrc );
Header ("Content-type: image/jpg ");
$ ImgWidth = $ imgwidth;
$ ImgHeight = $ imgheight;
// Create image and define colors
$ Imgsrc = imagecreatefromjpeg ($ imgsrc );
$ Image = imagecreatetruecolor ($ imgWidth, $ imgHeight); // create a colored Basemap
Imagecopyresampled ($ image, $ imgsrc, 0, 0, 0, 0, $ imgWidth, $ imgHeight, $ arr [0], $ arr [1]);
Imagepng ($ image );
Imagedestroy ($ image );
}
?>


Imagecopyresampled
Imagecopyresampled -- re-sample and copy part of the image and resize it.
Int imagecopyresampled (resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int DTH, int srcW, int srcH)
Imagecopyresampled () copies a square area of an image to another image and smoothly inserts the pixel value. Therefore, in particular, the image size is reduced, while the image definition is maintained. Dst_im and src_im are the identifiers of the target image and source Image respectively. If the width and height of the source and target are different, the image will be scaled and stretched accordingly. Coordinates refer to the upper left corner. This function can be used to copy the region within the same image (if dst_im and src_im are the same), but the result is unpredictable if the region overlaps.
Note: There is a problem with the color palette image limit (255 + 1 color. The re-sampling or filtering of images usually requires more than 255 colors. an approximate value is used to calculate the new re-sampling pixels and their colors. When you try to assign a new color to the palette image, if it fails, we select the color with the closest calculation result (theoretically. This is not always the closest visual color. This may produce weird results, such as blank (or visually blank) images. To skip this issue, use a true color image as the target image, for example, created with imagecreatetruecolor.
Note: imagecopyresampled () requires GD 2.0.l or higher.
A simple example:

The code is as follows:


// The file
$ Filename = 'test.jpg ';
$ Percent = 0.5;
// Content type
Header ('content-Type: image/jpeg ');
// Get new dimensions
List ($ width, $ height) = getimagesize ($ filename );
$ New_width = $ width * $ percent;
$ New_height = $ height * $ percent;
// Resample
$ Image_p = imagecreatetruecolor ($ new_width, $ new_height );
$ Image = imagecreatefromjpeg ($ filename );
Imagecopyresampled ($ image_p, $ image, 0, 0, 0, 0, $ new_width, $ new_height, $ width, $ height );
// Output
Imagejpeg ($ image_p, null, 100 );
?>


Example 2:

The code is as follows:


// The file
$ Filename = 'test.jpg ';
// Set a maximum height and width
$ Width = 200;
$ Height = 200;
// Content type
Header ('content-Type: image/jpeg ');
// Get new dimensions
List ($ width_orig, $ height_orig) = getimagesize ($ filename );
$ Ratio_orig = $ width_orig/$ height_orig;
If ($ width/$ height> $ ratio_orig ){
$ Width = $ height * $ ratio_orig;
} Else {
$ Height = $ width/$ ratio_orig;
}
// Resample
$ Image_p = imagecreatetruecolor ($ width, $ height );
$ Image = imagecreatefromjpeg ($ filename );
Imagecopyresampled ($ image_p, $ image, 0, 0, 0, 0, $ width, $ height, $ width_orig, $ height_orig );
// Output
Imagejpeg ($ image_p, null, 100 );
?>


There are two ways to change the image size:
The ImageCopyResized () function is valid in all GD versions, but its algorithm for scaling images is rough.
ImageCopyResamples (), whose edge obtained by the pixel interpolation algorithm is relatively smooth. (But this function is slower than ImageCopyResized ).
The parameters of the two functions are the same, as shown below:

The code is as follows:


ImageCopyResampled (dest, src, dy, dx, sx, sy, dw, dh, sw, sh );
ImageCopyResized (dest, src, dy, dx, sx, sy, dw, dh, sw, sh );


Example:

The code is as follows:


$ Src = imagecreatefrom#('php.jpg ');
$ Width = ImageSx ($ src );
$ Height = ImageSy ($ src );
$ X = $ widht/2;
$ Y = $ height/2;
$ Dst = ImageCreateTrueColor ($ x, $ y );
ImageCopyResampled ($ dst, $ src, 0, 0, 0, $ x, $ y, $ widht, $ height );
Header ('content-Type: image/png ');
ImagePNG ($ det );
?>


How to change the size of jpg image files in php

The code is as follows:


Function resize_jpg ($ img, $ w, $ h ){
// $ Thumb = imagecreate ($ w, $ h );
$ Image = imagecreatefromjpeg ($ img );
$ Imagedata = getimagesize ($ img );
If ($ h = "auto") $ h = $ w/($ imagedata [0]/$ imagedata [1]); // Obtain the height based on the aspect ratio of the source image!
$ Thumb = imagecreatetruecolor ($ w, $ h );
Imagecopyresized ($ thumb, $ image, 0, 0, 0, 0, $ w, $ h, $ imagedata [0], $ imagedata [1]);
Imagejpeg ($ thumb );
}
// Resize_jpg ($ file, $ w, $ h );
Resize_jpg ("images/dsc01244.jpg", 100,100 );
Imagedestory ($ thumb );
Imagedestory ($ image );
?>


Function code:

The code is as follows:


/*
* Image thumbnails
* $ Srcfile source image,
* $ Rate scaling ratio. the default value is half or the specific width pixel value.
* $ Filename: output image file name jpg
* Example: resizeimage ("zt32.gif", 0.1 );
* Example: resizeimage ("zt32.gif", 250 );
* Note: The function result is directly stored in the SRC attribute in the IMG tag of the HTML file.
*/
Function resizeimage ($ srcfile, $ rate =. 5, $ filename = ""){
$ Size = getimagesize ($ srcfile );
Switch ($ size [2]) {
Case 1:
$ Img = imagecreatefromgif ($ srcfile );
Break;
Case 2:
$ Img = imagecreatefromjpeg ($ srcfile );
Break;
Case 3:
$ Img = imagecreatefrompng ($ srcfile );
Break;
Default:
Exit;
}
// Width and height of the source image
$ Srcw = imagesx ($ img );
$ Srch = imagesy ($ img );
// The width and height of the target image
If ($ size [0] <= $ rate | $ size [1] <= $ rate ){
$ Dstw = $ srcw;
$ D…… = $ srch;
} Else {
If ($ rate <= 1 ){
$ Dstw = floor ($ srcw * $ rate );
$ D…… = floor ($ srch * $ rate );
} Else {
$ Dstw = $ rate;
$ Rate = $ rate/$ srcw;
$ D…… = floor ($ srch * $ rate );
}
}
// Echo "$ dstw, $ DTH, $ srcw, $ srch ";
// Create a true color image
$ Im = imagecreatetruecolor ($ dstw, $ dsomething );
$ Black = imagecolorallocate ($ im, 255,255,255 );
Imagefilledrectangle ($ im, 0, 0, $ dstw, $ dsomething, $ black );
Imagecopyresized ($ im, $ img, 0, 0, 0, $ dstw, $ DTH, $ srcw, $ srch );
// Output the image to a browser or file in JPEG format
If ($ filename ){
// Save and output the image
Imagejpeg ($ im, $ filename );
} Else {
// Output the image to the browser
Imagejpeg ($ im );
}
// Release the image
Imagedestroy ($ im );
Imagedestroy ($ img );
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.