PHP for image cropping and generating thumbnail program source code

Source: Internet
Author: User
Tags definition imagejpeg
Program | source code | Thumbnail processing method is:
1. When the original image of the width or height of any smaller than the specified size, only to carry out, such as abbreviated processing,
2. When the original image of the width and height are larger than the specified size, the first to be compared to the abbreviated processing, and then calculate the center position for cutting

The following is the source code:
<?php
/*
* $o _photo Original path
* $d _photo image path after processing
* $width definition width
* $height High Definition
* Call Method Cutphoto ("Test.jpg", "temp.jpg", 256,146);
*/

function Cutphoto ($o _photo, $d _photo, $width, $height) {

$temp _img = imagecreatefromjpeg ($o _photo);
$o _width = imagesx ($temp _img); Get the original image wide
$o _height = Imagesy ($temp _img); Get high

Judgment Processing method
if ($width > $o _width | | $height > $o _height) {//Huantuquan or higher than the specified size, compress

$newwidth = $o _width;
$newheight = $o _height;

if ($o _width> $width) {
$newwidth = $width;
$newheight = $o _height* $width/$o _width;
}

if ($newheight > $height) {
$newwidth = $newwidth * $height/$newheight;
$newheight = $height;
}

Thumbnail pictures
$new _img = Imagecreatetruecolor ($newwidth, $newheight);
Imagecopyresampled ($new _img, $temp _img, 0, 0, 0, 0, $newwidth, $newheight, $o _width, $o _height);
Imagejpeg ($new _img, $d _photo);
Imagedestroy ($new _img);


}else{//Huantuquan and height are larger than the specified size, after compression cut

if ($o _height* $width/$o _width> $height) {//Make sure the width is the same as the rule, if the height is larger than the specified, OK
$newwidth = $width;
$newheight = $o _height* $width/$o _width;
$x = 0;
$y = ($newheight-$height)/2;
}else{//Otherwise determine the height is the same as the specified, Width adaptive
$newwidth = $o _width* $height/$o _height;
$newheight = $height;
$x = ($newwidth-$width)/2;
$y = 0;
}

Thumbnail pictures
$new _img = Imagecreatetruecolor ($newwidth, $newheight);
Imagecopyresampled ($new _img, $temp _img, 0, 0, 0, 0, $newwidth, $newheight, $o _width, $o _height);
Imagejpeg ($new _img, $d _photo);
Imagedestroy ($new _img);

$temp _img = imagecreatefromjpeg ($d _photo);
$o _width = imagesx ($temp _img); Get thumbnail width
$o _height = Imagesy ($temp _img); Get thumbnail height

Crop a picture
$new _IMGX = Imagecreatetruecolor ($width, $height);
Imagecopyresampled ($new _imgx, $temp _img,0,0, $x, $y, $width, $height, $width, $height);
Imagejpeg ($new _imgx, $d _photo);
Imagedestroy ($new _IMGX);
}

}
?>


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.