欢迎光临胡政的个人博客站!

胡政个人网站-胡政个人博客

胡政个人博客网站

关注互联网和搜索引擎的技术博客

您当前的位置:主页 > php学习笔记 >

php不变形图片裁剪类,php图片裁剪,图片裁剪

发布时间:2016-02-16 14:30   点击:     编辑:admin

今天做了一个功能,根据前端传过来的图片信息和裁剪的坐标进行图片裁剪。百度了很久,一直没搞明白怎么做。
后来发现网站里别人给的类都是先让图片上传,然后取得上传的图片地址,然后再进行裁剪。
明白了这个过程之后,终于实现了裁剪功能。下面贴上代码

class PictureClipping{

    var $filepath;
    var $picname;
    var $x;
    var $y;
    var $w;
    var $h;
    var $tw;
    var $th;



    public function __construct($filepath, $picname,$x,$y,$w,$h,$tw,$th) {

        $this->filepath=$filepath;
        $this->picname=$picname;
        $this->x=$x;
        $this->y=$y;
        $this->w=$w;
        $this->h=$h;
        $this->tw=$tw;
        $this->th=$th;
    }

    public function crop(){

        $picname=$this->picname;
        $filepath=$this->filepath;
        $x=$this->x;
        $y=$this->y;
        $w=$this->w;
        $h=$this->h;
        $tw=$this->tw;
        $th=$this->th;

        $ext_s = explode(".",$picname);
        $ext = $ext_s[1];
        switch($ext){
            case "png":

                $image=imagecreatefrompng($picname);
                break;
            case "jpeg":

                $image=imagecreatefromjpeg($picname);
                break;
            case "jpg":

                $image=imagecreatefromjpeg($picname);
                break;
            case "gif":

                $image=imagecreatefromgif($picname);
                break;
        }

        $dst_r = ImageCreateTrueColor( $tw, $th );
        $this->setTransparency($image,$dst_r,$ext);
        imagecopyresampled($dst_r,$image,0,0,$x,$y,$tw,$th,$w,$h);
        imagedestroy($image);

        //$filep= C("UPLOAD")."/".$this->nodeId.'/';
        $file = MyHelper::getAbsolutePath($filepath.MyHelper::getFileName().".".$ext);

        switch($ext){
            case "png":
                imagepng($dst_r,($file != null ? $file : ''));
                break;
            case "jpeg":
                imagejpeg($dst_r,($file ? $file : ''),90);
                break;
            case "jpg":
                imagejpeg($dst_r,($file ? $file : ''),90);
                break;
            case "gif":
                imagegif($dst_r,($file ? $file : ''));
                break;
        }

        if(file_exists($file)){

            $returndata=array(
                "status"=>'1',
                "file"=>$file,
                "error"=>''
            );

        }else{
            $returndata=array(
                "status"=>'0',
                "file"=>'',
                "error"=>'生成文件出错!'
            );
        }

        return $returndata;

    }


    public function setTransparency($imgSrc,$imgDest,$ext){

        if($ext == "png" || $ext == "gif"){
            $trnprt_indx = imagecolortransparent($imgSrc);
            // If we have a specific transparent color
            if ($trnprt_indx >= 0) {
                // Get the original image's transparent color's RGB values
                $trnprt_color    = imagecolorsforindex($imgSrc, $trnprt_indx);
                // Allocate the same color in the new image resource
                $trnprt_indx    = imagecolorallocate($imgDest, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                // Completely fill the background of the new image with allocated color.
                imagefill($imgDest, 0, 0, $trnprt_indx);
                // Set the background color for new image to transparent
                imagecolortransparent($imgDest, $trnprt_indx);
            }
            // Always make a transparent background color for PNGs that don't have one allocated already
            elseif ($ext == "png") {
                // Turn off transparency blending (temporarily)
                imagealphablending($imgDest, true);
                // Create a new transparent color for image
                $color = imagecolorallocatealpha($imgDest, 0, 0, 0, 127);
                // Completely fill the background of the new image with allocated color.
                imagefill($imgDest, 0, 0, $color);
                // Restore transparency blending
                imagesavealpha($imgDest, true);
            }

        }
    }


}
这里实现的是php裁剪图片的功能,至于前端的html和js代码,就不贴出来了。网上也有很多插件。

最近访客

    热评文章