• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php图片缩放然后合成并保存

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
/**
 * 功能说明
 * 将files/model_24/images/下的所有图片文件进行等比例缩放 到$image40w 尺寸 然后放到files/model_24/model.png图片的指定位置后保存到files/model_24/after/下
 */
function model_24()
{
    //要求 货物图片大小为567x567
    $path = "files/model_24/";
    //得到该目录下面的所有文件
    $file=scandir($path."images/");
    foreach ($file as $index => $fileName)
    {
        //如果是文件夹则不继续执行
        if(trim(str_replace(".", "", $fileName)) == '')continue;
        //得到模板底图图片文件A
        $model = imagecreatefrompng($path."model.png");
        list($modelWidth,$modelHeight) = getimagesize($path."model.png");
        //得到需要放到底图上的图片B
        $productImage = file_get_contents($path."images/".$fileName);
        $productImage = imagecreatefromstring($productImage);
        list($productImageWidth,$productImageHeight) = getimagesize($path."images/".$fileName);
        
        //定义一个空白图片 也就是对图片B进行缩放
        $image40w = 45;//缩放后的尺寸
        $image40h = 45;
        $image40 = imagecreatetruecolor($image40w, $image40h);
        $white = imagecolorallocate($image40, 255, 255, 255);
        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;
        //将商品图片进行缩放
        imagecopyresized($image40, $productImage, $x, $y, 0, 0, $productImageWidthRsize, $productImageHeightRsize,$productImageWidth,$productImageHeight);
        imagecopymerge($model, $image40, 16, 552, 0, 0, $image40w, $image40h, 100);
        imagepng($model,$path."after/".$fileName);
    }
    echo "model_24()结束<br/>";
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php常用扩展有哪些发布时间:2022-07-10
下一篇:
phppost接口,注册功能发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap