function CreateImage ($SRCIMAGEURL, $DirImageUrl, $Width, $Height)
- {
- $img;
- $SRCW;
- $new _width;
- $srch;
- $new _height;
- Type of picture
- $type = substr (STRRCHR ($SrcImageUrl, "."), 1);
- Initializing an image
- if ($type = = "jpg")
- $img = Imagecreatefromjpeg ($SRCIMAGEURL);
- if ($type = = "gif")
- $img = Imagecreatefromgif ($SRCIMAGEURL);
- if ($type = = "png")
- $img = Imagecreatefrompng ($SRCIMAGEURL);
$SRCW = Imagesx ($img);
- $srch = Imagesy ($img);
if ($SRCW/$srch > $Width/$Height)
- {
- if ($SRCW > $Width)
- {
- $new _width = $Width;
- $new _height = $srch * ($Width/$SRCW);
- }
- Else
- {
- $new _width = $SRCW;
- $new _height = $srch;
- }
- }
- Else
- {
- if ($srch > $Height)
- {
- $new _height = $Height;
- $new _width = $SRCW * ($Height/$srch);
- }
- Else
- {
- $new _width = $SRCW;
- $new _height = $srch;
- }
- }
$new _image = Imagecreatetruecolor ($new _width, $new _height);
- Imagecopyresampled ($new _image, $img, 0, 0, 0, 0, $new _width, $new _height, $SRCW, $srch);
- Imagejpeg ($new _image, $DIRIMAGEURL);
Imagedestroy ($IMG);
- Imagedestroy ($new _image);
- }
Copy Code3. Post-Improvement code:
/*
* $o _photo Original path
* $d image path after _photo processing
* $width definition width
* $height defined High
* 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 width
$o _height = Imagesy ($temp _img); Get the original height
Judgment Processing method
if ($width > $o _width | | $height > $o _height) {//Huantuquan or higher than the specified size, compression
$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 image
$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{//original wide chart and high ratio of the specified size, compression after cutting
if ($o _height* $width/$o _width> $height) {//Make sure width is the same as specified, OK if height is greater than specified
$newwidth = $width;
$newheight = $o _height* $width/$o _width;
$x = 0;
$y = ($newheight-$height)/2;
}else{//Otherwise determined height is the same as the specified width adaptive
$newwidth = $o _width* $height/$o _height;
$newheight = $height;
$x = ($newwidth-$width)/2;
$y = 0;
}
Thumbnail image
$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);
- }
- }
- ?>
Copy Code |