Scale of images in PHP

Source: Internet
Author: User
Tags imagejpeg
In the picture save, we do not have to modify the image in PS or other tools, we can also use the code to implement. Here are two examples of the same effect:

$sourcefile = "images/1.jpg";//Old map path
$dstfile = "images/1_small.jpg";//New map path
$arr =getimagesize ($sourcefile);//Get the picture scale so that we don't have to look for the width of the image property again in the directory.

$SRC _width= "144";
$SRC _height= "200"; You can also customize the zoom scale but too much of the picture will distort

$scle =0.5;//Scale Factor
$DST _width=ceil ($arr [0]* $scle);
$DST _width=ceil ($arr [1]* $sclie);//new graph width High
$DST _img=imagecreatetruecolor ($dst _width, $dst _height);//Load new diagram
$SRC _img=imagecreatefromjpeg ($sourcefile);//load old diagram
Imagecopyresampled ($dst _img, $src _img,0,0,0,0$dst_width, $dst _height, $src _width, $src _height);//Output image
Imagejpeg ($dst _img, $dstfile);//new diagram, Path
Imagedestroy ($dst _img);
Imagedestroy ($src _img);//Destroy Picture

?>

Class zoom{
private $srcImg;//Original address
private $scle;//Zoom factor
Public function __construct ($a $b) {
$this->srcimg= $a;
$this->scle= $b;
List ($src _w $src _h) =getimagesize ($this->srcimg);
$DST _w=ceil ($src _w* $this->scle);
$DST _h=ceil ($src _h* $this->scle);
$arr =explode ("." $this->srcimg);
$ext =end ($arr);
if ($ext = = "jpg") {
$src _img=imagecreatefromjpeg ($this->srcimg);
}else if ($ext = = "gif") {
$src _img=imagecreatefromgif ($this->srcimg);
}else if ($ext = = "png") {
$src _img=imagecreatefrompng ($this->srcimg);
}else{
echo "Please select JPG, PNG, GIF image format";
Exit
}
$DSTIMG = $arr [0]. $this->scle. " _small ".". ". $ext;
$DST _img=imagecreatetruecolor ($dst _w$dst_h);
Imagecopyresampled ($dst _img$src_img0000$dst_w$dst_h$src_w$src_h);
if ($ext = = "jpg") {
Imagejpeg ($dst _img$dstimg);
}else if ($ext = = "gif") {
Imagegif ($dst _img$dstimg);
}else if ($ext = = "png") {
Imagepng ($dst _img$dstimg);
}
Imagedestroy ($src _img);
Imagedestroy ($dst _img);
}
}
$image = "Imgs/a.jpg";
$b = 1;
$i =new Zoom ($image $b);
?>

  • 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.