PHP等比压缩图片并合成到另一张图指定的区域

京漂大叔 2020-12-23 PM 3069℃ 0条

pexels-photo-5644323.jpeg
最近要批量处理一批图片,将一个图案放到空白的T恤上,图案的大小又不固定。

说下处理的思路
1、用PS打开空白的T恤,通过矩形工具在空白的T恤上,要放置图案的区域画一个图形,然后打开窗口->信息面板,将鼠标放置在刚才画的矩形图形的左上角,获取到矩形图左上角的x,y坐标,在获取到右上角的X,y左边,和左下角x,y坐标。这样就可以计算出矩形区域的最大宽和最大高。图案就放到这个区域就行。

2、通过上面获取到的最大宽和最大高,创建画布,用于缩放图案,符合矩形区域大小。

3、获取图案图片的宽和高,然后和最大宽和最大高进行比较,等比缩放图案图片。

4、最后将缩放之后的图案图片根据获取到的x,y坐标和空白的T恤进行合并即可。

 function hebingimg($dst_path,$src_path){
        $logo_path = 'calcalogosmall.png';
        $Absolute_Path=dirname(__FILE__);
        $sp = explode('.',$dst_path);
        //print_r($sp);die;
        $p = explode("\\",$src_path);
        $newp = 'png';
        $Absolute_Path = $p[0].'\\'.$p[1].'\\'.$newp.'\\'.$p[3].'\\'.$sp[0].'\\';
        if(!is_dir($Absolute_Path)){
            mkdir($Absolute_Path,0777,true);
        } //die;
       //图案放置到空白T恤区域的左上角位置
        $dst_weizhi_one_x = '374';
        $dst_weizhi_one_y = '341';
        //左下角的位置
        $dst_weizhi_two_x = '374';
        $dst_weizhi_two_y = '559';
        //右上角的位置
        $dst_weizhi_three_x = '611';
        $dst_weizhi_three_y = '341';
        //右下角的位置
        $dst_weizhi_four_x = '611';
        $dst_weizhi_four_y = '559';
        //LOGO信息
        $logo_width_x = '302';
        $logo_height_y = '262';
        $logo_width = 96;
        $logo_height = 69;
        
        $max_width = $dst_weizhi_three_x-$dst_weizhi_one_x;   //区域最大宽
        $max_height = $dst_weizhi_two_y-$dst_weizhi_one_y;    //区域最大高
        $hz = substr(strrchr($dst_path, '.'), 1);
        
        
    
        //生成新图片名
        $image = $Absolute_Path.date("YmdHis").rand(1000,9999).".".$hz;
        //echo $image;die;
        //得到logo图片
        $logoimg = imagecreatefromstring(file_get_contents($logo_path));
        //得到空白T恤
        $model = imagecreatefromjpeg($dst_path);
        list($modelWidth,$modelHeight) = getimagesize($dst_path);
        //得到需要放到空白T恤的图案
        $productImage = file_get_contents($src_path);
        $productImage = imagecreatefromstring($productImage);
        list($productImageWidth,$productImageHeight) = getimagesize($src_path);
    
        //定义一个空白图片 也就是对图片B进行缩放
        $image40w = $max_width;//缩放后的尺寸
        $image40h = $max_height;
        $image40 = imagecreatetruecolor($image40w, $image40h);
        $white = imagecolorallocatealpha($image40, 0, 0, 0, 127);
        imagefill($image40, 0, 0, $white);
   
        $productImageHeightRsize = 0;
        $productImageWidthRsize = 0;
        //如果货物的宽度大于高度 那么宽度定死为$image40size 否则就是高度定死$image40size
        if($productImageWidth > $productImageHeight)
        {
            $scale = $image40w / $productImageWidth;
            $productImageHeightRsize = $scale * $productImageHeight;
            $productImageWidthRsize = $image40w;
        }
        else 
        {
            $scale = $image40h / $productImageHeight;
            $productImageWidthRsize = $scale * $productImageWidth;
            $productImageHeightRsize = $image40h;
        }
        //定义货物图片坐标 以便左右与上下居中
        $x = ($image40w - $productImageWidthRsize) / 2;
        $y = ($image40h - $productImageHeightRsize) / 2;
        //将商品图片进行缩放
        imagecopyresampled($image40, $productImage, $x, $y, 0, 0, $productImageWidthRsize, $productImageHeightRsize,$productImageWidth,$productImageHeight);
        //分配颜色 + alpha,将颜色填充到新图上
        imagesavealpha($image40, true);
        
        imagecopy($model, $image40, $dst_weizhi_one_x, $dst_weizhi_one_y, 0, 0, $image40w, $image40h);
        
        imagecopy($model, $logoimg, $logo_width_x, $logo_height_y, 0, 0, $logo_width, $logo_height);//logo
        imagepng($model, $image); //生成合并后的图
    }

非特殊说明,文章均为原创。

评论啦~