Heim  >  Artikel  >  php教程  >  GD库使用小结---2,gd小结---2

GD库使用小结---2,gd小结---2

WBOY
WBOYOriginal
2016-06-13 09:05:38963Durchsuche

GD库使用小结---2,gd小结---2

  接着上一篇。GD库可以折腾很多用法出来,当然得跟画图相关,除了前面的验证码、水印外,还可以进行图片的缩放,裁剪、旋转等操作,这在很多应用中可以见到。

  1. 加水印

  前面已经知道,我们可以使用imagechar或者imagestring等将字符或字符串(甚至中文字符)绘制到图像上,以达到水印的目的,还有个更好的方式,不仅能加字符水印,还能加图片水印:imagecopy。

  原型:bool imagecopy (resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h),看名字知道这是复制,第1、2个参数分别是目标图像句柄、源文件句柄,加水印时,如果水印图片是一张小图,加在一张大图上面,那么第一个参数就是大图句柄,第二个参数就是小图句柄。第3、4个参数是水印在目标图像上的x、y坐标值,第5、6个参数是水印图片上开始的x、y坐标值,第7、8个参数是水印图片即将要作为水印的宽和高,因此这个方法的意思就是,将水印图像src_im上左上角顶点坐标为(src_x, src_y)处,宽和高分别为src_w、src_h的部分复制到图像dst_im上,再将这个dst_im图像画到画布上保存或输出,即为加过水印后的图片了,代码:

<?<span>php
    
    date_default_timezone_set(</span>'Asia/Shanghai'<span>);
    </span><span>define</span>('DS',<span> DIRECTORY_SEPARATOR);
    </span><span>//</span><span> 加水印,文字、图片水印,以图片为例</span>
    <span>function</span> watermark(<span>$srcFile</span> = '',  <span>$markFile</span> = '', <span>$dstFile</span> = ''<span>)
    {
        </span><span>if</span>(!<span>file_exists</span>(<span>$srcFile</span>) || !<span>file_exists</span>(<span>$markFile</span><span>))
        {
            </span><span>echo</span> 'file not exists!<br/>'<span>;
            </span><span>return</span> <span>false</span><span>;
        }
        </span><span>//</span><span> 获取原始图片与水印图片的宽高</span>
        <span>list</span>(<span>$srcWidth</span>, <span>$srcHeight</span>) = <span>getimagesize</span>(<span>$srcFile</span><span>);
        </span><span>list</span>(<span>$markWidth</span>, <span>$markHeight</span>) = <span>getimagesize</span>(<span>$markFile</span><span>);
        </span><span>//</span><span> 水印图片不能比原始图片像素还大</span>
        <span>if</span>(<span>$markWidth</span> > <span>$srcWidth</span> || <span>$markHeight</span> > <span>$srcHeight</span><span>)
        {
            </span><span>return</span> <span>false</span><span>;
        }
        </span><span>//</span><span> 获取即将被加水印的原始图片句柄、水印图片句柄</span>
        <span>$dstImg</span> = imagecreatefromjpeg(<span>$srcFile</span><span>);
        </span><span>$markImg</span> = imagecreatefrompng(<span>$markFile</span><span>);
        </span><span>//</span><span> 加水印的位置,简单放在右下角</span>
        <span>$dst_x</span> = <span>$srcWidth</span> - <span>$markWidth</span><span>;
        </span><span>$dst_y</span> = <span>$srcHeight</span> - <span>$markHeight</span><span>;
        </span><span>//</span><span> 获取文件信息</span>
        <span>$fileinfo</span> = <span>pathinfo</span>(<span>$srcFile</span><span>);
        </span><span>if</span>(<span>empty</span>(<span>$dstFile</span><span>))
        {
            </span><span>$dstFile</span> = <span>rtrim</span>(<span>$fileinfo</span>['dirname'], DS).DS.'mark_'.<span>$fileinfo</span>['filename'].<span>date</span>('YmdHis').<span>mt_rand</span>(1, 1000).'.jpeg'<span>;
        }
        </span><span>//</span><span> 将水印图片复制到已有图片上</span>
        imagecopy(<span>$dstImg</span>, <span>$markImg</span>, <span>$dst_x</span>, <span>$dst_y</span>, 0, 0, <span>$srcWidth</span>, <span>$srcHeight</span><span>);
        </span><span>//</span><span> 将新加完水印的图片保存起来</span>
        imagejpeg(<span>$dstImg</span>, <span>$dstFile</span><span>);
        imagedestroy(</span><span>$dstImg</span><span>);
        imagedestroy(</span><span>$markImg</span><span>);
        </span><span>return</span> <span>true</span><span>;
    }
    
    </span><span>$srcFile</span> = 'G:\wamp\www\html\image\p125.jpg';  <span>//</span><span> 原图片</span>
    <span>$markFile</span> = 'G:\wamp\www\html\image\ooopic_5.png'; <span>//</span><span> 水印图片</span>
    watermark(<span>$srcFile</span>, <span>$markFile</span>);

  效果:      

在这里,简单将水印图片放在图片右下角,所以放在图片的右下角要一个简单的计算,调用imagecopy时,从水印图片的左上角顶点(坐标0,0)开始全部(宽高传入水印图片宽高)复制到待加水印图片上,然后imagejpeg绘制图像并保存,注意类似imagejpeg(这里简单使用它)等绘图函数传入第二个参数时是保存图片为一个文件,而不是输出到浏览器,所以也不需调header函数发送头信息。

  当然还要搞清楚哪是源文件(src),哪是目标文件(dst),把一张小图加水印到一张大图上时,源文件是小图水印,把它复制到大图上,大图就是目标。

   能用图片作为水印,在于imagecopy的第二个参数是图像句柄,所以可以从现有图片来创建一个(如imagecreatefromjpeg),当然也能从现有的字符创建一个字符图像句柄变量---使用imagecreatefromstring方法,所以这个更通用。

  2. 图片缩放

  很多应用,图片列表是小图,当你点击某一个时,才会展现完整大图,这涉及到一个图片收缩的处理。有两个方法可供使用:imagecopyresized和imagecopyresampled,几乎一样,不同的是后者对图片进行重采样(貌似是我专业的词),所以成图质量更好(重采样的话也得看采取哪种方法,有的会变得更渣),挑一个说:

  bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

  第一、二个参数与上面相似,如何做到缩放?比如这里,是将原图像上左上角顶点处,坐标为(src_x, src_y),宽高为src_w、src_h的部分图片,画到目标图像上坐标为(dst_x, dst_y),宽高为dst_w、dst_h的地方,所以如果目标图片上的宽高比原图上选取部分的宽高小的话,就成了缩小版了,当然还需要两个左上角顶点的坐标值相同,以jpg类型为例:

<?<span>php
    date_default_timezone_set(</span>'Asia/Shanghai'<span>);
    </span><span>define</span>('DS',<span> DIRECTORY_SEPARATOR);
    </span><span>//</span><span> 缩小图片</span>
    <span>/*</span><span>*
     * @param src 原图像路径
     * @param percent 缩小比例
     * @param dstFile 保存图片的路径
     </span><span>*/</span>
    <span>function</span> zoomPic(<span>$srcFile</span> = '', <span>$percent</span> = 0.5, <span>$dstFile</span> = ''<span>)
    {
        </span><span>if</span>(!<span>file_exists</span>(<span>$srcFile</span><span>))
        {
            </span><span>return</span> <span>false</span><span>;
        }

        </span><span>list</span>(<span>$width</span>, <span>$height</span>) = <span>getimagesize</span>(<span>$srcFile</span>); <span>//</span><span> 获取宽高</span>
        (<span>$percent</span> <= 0 || <span>$percent</span> > 1) && <span>$percent</span> = 0.5<span>;
        </span><span>$newWidth</span> = <span>floor</span>(<span>$width</span> * <span>$percent</span>);    <span>//</span><span> 缩小后的宽高</span>
        <span>$newHeight</span> = <span>floor</span>(<span>$height</span> * <span>$percent</span><span>);
        
        </span><span>$dstImg</span> = imagecreatetruecolor(<span>$newWidth</span>, <span>$newHeight</span>); <span>//</span><span> 创建新图像宽高的画布</span>
        <span>$srcImg</span> = imagecreatefromjpeg(<span>$srcFile</span>);  <span>//</span><span> 从原图像文件创建画布</span>
        
        <span>$pathinfo</span> = <span>pathinfo</span>(<span>$srcFile</span><span>);
        </span><span>if</span>(!<span>$dstFile</span>) <span>$dstFile</span> = <span>rtrim</span>(<span>$pathinfo</span>['dirname'], DS).DS.'zoom_'.<span>$pathinfo</span>['filename'].<span>date</span>('YmdHis').<span>mt_rand</span>(1, 1000).".jpeg"<span>;
        </span><span>//</span><span> 从源文件左上角顶点开始进行缩小
        //imagecopyresized($dstImg, $srcImg, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); </span>
        imagecopyresampled(<span>$dstImg</span>, <span>$srcImg</span>, 0, 0, 0, 0, <span>$newWidth</span>, <span>$newHeight</span>, <span>$width</span>, <span>$height</span><span>); 
        </span><span>//</span><span> 绘制且保存图像</span>
        imagejpeg(<span>$dstImg</span>, <span>$dstFile</span><span>);
        </span><span>//</span><span> 销毁资源</span>
        imagedestroy(<span>$dstImg</span><span>);
        imagedestroy(</span><span>$srcImg</span><span>);
        </span><span>return</span> <span>true</span><span>;
    }
    </span><span>//</span><span> 测试</span>
    <span>$srcFile</span> = 'G:\wamp\www\html\image\p179.jpg'<span>;
    zoomPic(</span><span>$srcFile</span>);

  效果:  

  在这里,我们可以将原图与小图分两套存放,列表展示小图,低级查就展示原图。

  3. 图片裁剪

  考虑上面的imagecopyresampled方法,如果不从原图像的左上角开始,而是左上角偏右下某一点开始,切图的宽高也再等于源文件完整的宽高,而是部分宽高,那么新的重采样的图片,就是源图片的一部分,就达到了裁剪的效果,所以裁剪与缩放使用的方法一样

  

<?<span>php
    date_default_timezone_set(</span>'Asia/Shanghai'<span>);
    </span><span>define</span>('DS',<span> DIRECTORY_SEPARATOR);
    </span><span>//</span><span> cut a picture</span>
    <span>function</span> cutPic(<span>$srcFile</span> = '', <span>$x</span> = 0, <span>$y</span> = 0, <span>$width</span> = 16, <span>$height</span> = 16, <span>$dstFile</span> = ''<span>)
    {
        </span><span>if</span>(!<span>file_exists</span>(<span>$srcFile</span><span>))
        {
            </span><span>return</span> <span>false</span><span>;
        }
        </span><span>list</span>(<span>$srcWidth</span>, <span>$srcHeight</span>, <span>$type</span>) = <span>getimagesize</span>(<span>$srcFile</span><span>);
        </span><span>$x</span> < 0 && <span>$x</span> = 0<span>;
        </span><span>$y</span> < 0 && <span>$y</span> = 0<span>;
        </span><span>//</span><span> 宽高设置</span>
        ((<span>$width</span> + <span>$x</span>) > <span>$srcWidth</span>) && <span>$width</span> = <span>$srcWidth</span><span>; 
        ((</span><span>$height</span> + <span>$y</span>) > <span>$srcHeight</span>) && <span>$height</span> = <span>$srcHeight</span><span>;
        (</span><span>$width</span> <= 0) && <span>$width</span> = <span>$srcWidth</span><span>; 
        (</span><span>$height</span> <= 0) && <span>$height</span> = <span>$srcHeight</span><span>;

        </span><span>$dstImg</span> = imagecreatetruecolor(<span>$width</span>, <span>$height</span>);  <span>//</span><span> 目标文件资源 </span>
        <span>switch</span>(<span>$type</span><span>)
        {
            </span><span>case</span> IMG_GIF:
                <span>$srcImg</span> = imagecreatefromgif(<span>$srcFile</span>);  <span>//</span><span> 获取源文件资源句柄及扩展名处理,以使用合适的函数和扩展</span>
                <span>$ext</span> = 'gif'<span>;
                </span><span>$imagefun</span> = 'imagegif'<span>;
                </span><span>break</span><span>;
            </span><span>case</span> IMG_JPG:
                <span>$srcImg</span> = imagecreatefromjpeg(<span>$srcFile</span><span>);  
                </span><span>$ext</span> = 'jpeg'<span>;
                </span><span>$imagefun</span> = 'imagejpeg'<span>;
                </span><span>break</span><span>;
            </span><span>default</span>:
                <span>$srcImg</span> = imagecreatefrompng(<span>$srcFile</span><span>);  
                </span><span>$ext</span> = 'png'<span>;
                </span><span>$imagefun</span> = 'imagepng'<span>;
                </span><span>break</span><span>;
        }
        </span><span>//</span><span> 设置保存剪切后的文件路径</span>
        <span>$fileinfo</span> = <span>pathinfo</span>(<span>$srcFile</span><span>);
        </span><span>if</span>(<span>empty</span>(<span>$dstFile</span><span>))
        {
            </span><span>$dstFile</span> = <span>rtrim</span>(<span>$fileinfo</span>['dirname'], DS).DS.'cut_'.<span>$fileinfo</span>['filename'].<span>date</span>('YmdHis').<span>mt_rand</span>(1, 1000).".{<span>$ext</span>}"<span>;
        }
        </span><span>//</span><span> 执行剪切操作</span>
        imagecopyresampled(<span>$dstImg</span>, <span>$srcImg</span>, 0, 0, <span>$x</span>, <span>$y</span>, <span>$width</span>, <span>$height</span>, <span>$width</span>, <span>$height</span><span>);
        </span><span>//</span><span> 画于画布并保存文件</span>
        <span>$imagefun</span>(<span>$dstImg</span>, <span>$dstFile</span><span>);
        imagedestroy(</span><span>$dstImg</span><span>);
        imagedestroy(</span><span>$srcImg</span><span>);
        </span><span>return</span> <span>true</span><span>;
    }
    </span><span>//</span><span> 测试</span>
    <span>$srcFile</span> = 'G:\wamp\www\html\image\p221.jpg'<span>;
    cutPic(</span><span>$srcFile</span>, 50, 50, 50, 50);

  效果:    

  常见的应用是,我们在给自己的某个应用换头像时,头像太大,就会用到裁剪,用这就可以做一个模拟实现。

  4. 图片旋转

  图片旋转也十分常见,主要用到函数imagerotate,原型:resource imagerotate ( resource $image , float $angle , int $bgd_color [, int $ignore_transparent = 0 ] ),第一个参数是待旋转图像句柄,第二个参数angle 是旋转的角度数值,第三个参数指定一个颜色,即当旋转后出现空的地方是使用哪种颜色填充,第四个参数是指定一个透明色,默认0表示保留透明色。该方法返回一个新的图像句柄,就是经过旋转后的图像资源变量,将它绘制保存即可。还要注意的是,旋转的角度可以指定0到360之间,为逆时针旋转,以jpg为例:

<?<span>php
    
    date_default_timezone_set(</span>'Asia/Shanghai'<span>);
    </span><span>define</span>('DS',<span> DIRECTORY_SEPARATOR);
    </span><span>/*</span><span>*
     * 图片旋转
     * @param angular 旋转角度值 0-360
     </span><span>*/</span>
    <span>function</span> rotatePic(<span>$srcFile</span> = '', <span>$angular</span> = 0, <span>$dstFile</span> = ''<span>)
    {
        </span><span>if</span>(!<span>file_exists</span>(<span>$srcFile</span><span>))
        {
            </span><span>echo</span> 'file not exists<br/>'<span>;
            </span><span>return</span> <span>false</span><span>;
        }

        </span><span>$srcImg</span> = imagecreatefromjpeg(<span>$srcFile</span><span>);
        </span><span>//</span><span> 处理保存文件地址</span>
        <span>$fileinfo</span> = <span>pathinfo</span>(<span>$srcFile</span><span>);
        </span><span>if</span>(<span>empty</span>(<span>$dstFile</span><span>))
        {
            </span><span>$dstFile</span> = <span>rtrim</span>(<span>$fileinfo</span>['dirname'], DS).DS.'rotate_'.<span>$fileinfo</span>['filename'].<span>date</span>('YmdHis').<span>mt_rand</span>(1, 1000).'.jpeg'<span>;
        }
        
        </span><span>$white</span> = imagecolorallocate(<span>$srcImg</span>, 0xff, 0xff, 0xf1<span>);
        </span><span>//</span><span> 执行旋转,注意是逆时针方向</span>
        <span>$dstImg</span> = imagerotate(<span>$srcImg</span>, <span>$angular</span>, <span>$white</span><span>);  
        </span><span>//</span><span> 画到画布,保存文件</span>
        imagejpeg(<span>$dstImg</span>, <span>$dstFile</span><span>);
        imagedestroy(</span><span>$dstImg</span><span>);
        imagedestroy(</span><span>$srcImg</span><span>);
        </span><span>return</span> <span>true</span><span>;
    }
    </span><span>//</span><span> 测试</span>
    <span>$srcFile</span> = 'G:\wamp\www\html\image\p219.jpg'<span>;
    rotatePic(</span><span>$srcFile</span>, 220);

  效果:原图     旋转后

  5. 图片翻转

  这个在应用中不那么常见。所谓翻转,就是对图像进行镜面翻转,比如以图片中间竖直线为轴线,左边换到右边,右边换到左边,对调一下位置,就是左右翻转。想象一下,以中间竖直线为对称轴的情况,Y轴像素点不变,X轴上的像素点左右对调,仍可以使用imagecopy方法,对于源文件,在复制到目标图像时,进行这个操作,以绕Y轴旋转,jpg类型图片为例

<?<span>php
    </span><span>//</span><span> 图片翻转</span>
    date_default_timezone_set('Asia/Shanghai'<span>);
    </span><span>define</span>('DS',<span> DIRECTORY_SEPARATOR);
    </span><span>//</span><span> 沿Y轴翻转,x坐标值对调</span>
    <span>function</span> turnY(<span>$srcFile</span> = '', <span>$dstFile</span> = ''<span>)
    {
        </span><span>if</span>(!<span>file_exists</span>(<span>$srcFile</span><span>))
        {
            </span><span>return</span> <span>false</span><span>;
        }
        </span><span>//</span><span> 原图像句柄和宽高获取</span>
        <span>$srcImg</span> = imagecreatefromjpeg(<span>$srcFile</span><span>);
        </span><span>$srcWidth</span> = imagesx(<span>$srcImg</span><span>);
        </span><span>$srcHeight</span> = imagesy(<span>$srcImg</span><span>);
        
        </span><span>$dstImg</span> = imagecreatetruecolor(<span>$srcWidth</span>, <span>$srcHeight</span><span>);
        </span><span>//</span><span> 沿Y轴翻转,x轴上的像素点左右对调</span>
        <span>for</span>(<span>$i</span> = 0; <span>$i</span> < <span>$srcWidth</span>; <span>$i</span>++<span>)
        {
            imagecopy(</span><span>$dstImg</span>, <span>$srcImg</span>, <span>$srcWidth</span>-<span>$i</span>-1, 0, <span>$i</span>, 0, 1, <span>$srcHeight</span><span>);
        }
        </span><span>//</span><span> 画像保存路径处理</span>
        <span>$fileinfo</span> = <span>pathinfo</span>(<span>$srcFile</span><span>);
        </span><span>if</span>(<span>empty</span>(<span>$dstFile</span><span>))
        {
            </span><span>$dstFile</span> = <span>rtrim</span>(<span>$fileinfo</span>['dirname'], DS).DS.'turnx_'.<span>$fileinfo</span>['filename'].<span>date</span>('YmdHis').<span>mt_rand</span>(1, 1000).'.jpeg'<span>;
        }
        </span><span>//</span><span> 绘制图像,保存文件</span>
        imagejpeg(<span>$dstImg</span>, <span>$dstFile</span><span>);
        imagedestroy(</span><span>$dstImg</span><span>);
        imagedestroy(</span><span>$srcImg</span><span>);
        </span><span>return</span> <span>false</span><span>;
    }    
    </span><span>//</span><span> 测试</span>
    <span>$srcFile</span> = 'G:\wamp\www\html\image\p311.jpg'<span>;
    turnY(</span><span>$srcFile</span>);

  效果: 翻转前  翻转后 

  主要就是for循环那儿,得到源文件的宽度$srcWidth后,如果源文件上坐标是($i, 0)则对应目标图像上坐标($srcWidth-$i-1, 0),然后将宽度为1个像素,高为源文件整个高度$srcHeight的资源复制过去,循环完成后就全部复制到一个图片上了,相当于是一条一条线的画过去的。

  无意浏览手册感觉被坑,看到imageflip函数猜到是这个功能,一看果然是的,可是看的书已经落后几年了,原型:bool imageflip ( resource $image , int $mode ),第一个参数是目标图像资源,第二个参数是翻转的方式,使用php自带的枚举变量即可,有IMG_FLIP_HORIZONTAL(水平)、IMG_FLIP_HORIZONTAL(竖直)、IMG_FLIP_BOTH(水平竖直)三种方式,一个函数即可实现。

  反正都挺简单,不如练练手玩玩 :-D

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn