zoukankan      html  css  js  c++  java
  • php 图片缩放然后合成并保存

    /**
     * 功能说明
     * 将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/>";
    }
  • 相关阅读:
    python 读csv文件时,在csv类型上执行类型转换
    python 读csv文件对列名进行合法性验证
    python 读csv数据 通过改变分隔符去掉引号
    python 写csv文件
    python 简单了解namedtuple
    python 读csv格式的文件
    python 序列化,反序列化
    PyQt5 笔记(05):信号/槽
    PyQt5 笔记(04):主窗口卡死问题
    PyQt5 笔记(03):弹出窗口大全
  • 原文地址:https://www.cnblogs.com/potatog/p/7026652.html
Copyright © 2011-2022 走看看