} } }

    php图像处理惩罚函数大全(缩放、剪裁、缩放、翻转、扭转、透明、锐化的实例总结)

    添加时间:2013-7-9 点击量:

    php处理惩罚代码分享,包含缩放、剪裁、缩放、翻转、扭转、透明、锐化等。


    一、创建资料
    imagecreatetruecolor(width,height);
    imagecreategif(名称);
    imagecreatepng(名称);
    imagecreatejpeg(名称);画出各类图像 imagegif(资料,保存路径);
    imagepng()
    imagejpeg();

    二、获取属性
    imagesx(res//宽度
    imagesy(res//高度
    getimagesize(文件路径)
    返回一个具有四个单位的数组。索引 0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标识表记标帜:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。这些标识表记标帜与 PHP 4.3.0 新加的 IMAGETYPE 常量对应。索引 3 是文本字符串,内容为“height=yyy width=xxx”,可直接用于 IMG 标识表记标帜。
    烧毁图像资料
    imagedestroy(资料);

    三、透明处理惩罚
    PNG、jpeg透明色都正常,只有gif不正常
    imagecolortransparent(resource image [,int color])//将某个色彩设置成透明色
    imagecolorstotal()
    imagecolorforindex();

    四、的裁剪
    imagecopyresized()
    imagecopyresampled();

    五、加水印(文字、)
    字符串编码转换string iconv ( string ¥in_charset , string ¥out_charset , string ¥str )

    六、扭转
    imagerotate();//制订角度的翻转

    七、的翻转
    沿X轴 沿Y轴翻转

    八、锐化
    imagecolorsforindex()
    imagecolorat()
    在上画图形 ¥img=imagecreategif(./images/map.gif);



     1 <?PHP
    
    2 /
    3 锐化处理惩罚
    4 edit by www.jbxue.com
    5 /
    6 ¥red= imagecolorallocate(¥img, 255, 0, 0);
    7
    8 imageline(¥img, 0, 0, 100, 100, ¥red);
    9 imageellipse(¥img, 200, 100, 100, 100, ¥red);
    10 imagegif(¥img, ./images/map2.gif);
    11 imagedestroy(¥img);
    12 通俗缩放
    13 复制代码 代码如下:
    14
    15 ¥filename=./images/hee.jpg;
    16 ¥per=0.3;
    17 list¥width¥height)=getimagesize¥filename);
    18 ¥n_w=¥width¥per;
    19 ¥n_h=¥width¥per;
    20 ¥new=imagecreatetruecolor(¥n_w¥n_h);
    21 ¥img=imagecreatejpeg(¥filename);
    22 //拷贝项目组图像并调剂
    23 imagecopyresized(¥new¥img,0, 0,0, 0,¥n_w¥n_h¥width¥height);
    24 //图像输出新、另存为
    25 imagejpeg(¥new, ./images/hee2.jpg);
    26 imagedestroy(¥new);
    27 imagedestroy(¥img);
    28
    29 等比例缩放、没处理惩罚透明色
    30 复制代码 代码如下:
    31
    32 function thumn(¥background¥width¥height¥newfile) {
    33 list¥s_w¥s_h)=getimagesize¥background);//获取原高度、宽度
    34 if¥width && (¥s_w < ¥s_h)) {
    35 ¥width = (¥height / ¥s_h¥s_w;
    36 } else {
    37 ¥height = (¥width / ¥s_w¥s_h;
    38 }
    39 ¥new=imagecreatetruecolor(¥width¥height);
    40 ¥img=imagecreatejpeg(¥background);
    41 imagecopyresampled(¥new¥img, 0, 0, 0, 0, ¥width¥height¥s_w¥s_h);
    42 imagejpeg(¥new¥newfile);
    43 imagedestroy(¥new);
    44 imagedestroy(¥img);
    45 }
    46 thumn(images/hee.jpg, 200, 200, ./images/hee3.jpg);
    47
    48 gif透明色处理惩罚
    49 复制代码 代码如下:
    50
    51 function thumn(¥background¥width¥height¥newfile) {
    52 list¥s_w¥s_h)=getimagesize¥background);
    53 if¥width && (¥s_w < ¥s_h)) {
    54 ¥width = (¥height / ¥s_h¥s_w;
    55 } else {
    56 ¥height = (¥width / ¥s_w¥s_h;
    57 }
    58 ¥new=imagecreatetruecolor(¥width¥height);
    59 ¥img=imagecreategif(¥background);
    60 ¥otsc=imagecolortransparent(¥img);
    61 if¥otsc >=0 && ¥otst < imagecolorstotal(¥img)){//断定索引色
    62 ¥tran=imagecolorsforindex(¥img¥otsc);//索引色彩值
    63 ¥newt=imagecolorallocate(¥new¥tran[red], ¥tran[green], ¥tran[blue]);
    64 imagefill(¥new, 0, 0, ¥newt);
    65 imagecolortransparent(¥new¥newt);
    66 }
    67 imagecopyresized(¥new¥img, 0, 0, 0, 0, ¥width¥height¥s_w¥s_h);
    68 imagegif(¥new¥newfile);
    69 imagedestroy(¥new);
    70 imagedestroy(¥img);
    71 }
    72 thumn(images/map.gif, 200, 200, ./images/map3.gif);


    裁剪



    <?php
    
    /
    裁剪处理惩罚
    edit by www.jbxue.com
    /

    function cut(¥background¥cut_x¥cut_y¥cut_width¥cut_height¥location){
    ¥back=imagecreatejpeg(¥background);
    ¥new=imagecreatetruecolor(¥cut_width¥cut_height);
    imagecopyresampled(
    ¥new¥back, 0, 0, ¥cut_x¥cut_y¥cut_width¥cut_height¥cut_width¥cut_height);
    imagejpeg(
    ¥new¥location);
    imagedestroy(
    ¥new);
    imagedestroy(
    ¥back);
    }
    cut(
    ./images/hee.jpg, 440, 140, 117, 112, ./images/hee5.jpg);
    ?>


    加水印 文字水印



     1 <?PHP
    
    2 /
    3
    4 添加文字水印
    5 /
    6
    7 function mark_text(¥background¥text¥x¥y){
    8 ¥back=imagecreatejpeg(¥background);
    9 ¥color=imagecolorallocate(¥back, 0, 255, 0);
    10 imagettftext(¥back, 20, 0, ¥x¥y¥color, simkai.ttf, ¥text);
    11 imagejpeg(¥back, ./images/hee7.jpg);
    12 imagedestroy(¥back);
    13 }
    14 mark_text(./images/hee.jpg, 细说PHP, 150, 250);
    15 //水印
    16 function mark_pic(¥background¥waterpic¥x¥y){
    17 ¥back=imagecreatejpeg(¥background);
    18 ¥water=imagecreategif(¥waterpic);
    19 ¥w_w=imagesx(¥water);
    20 ¥w_h=imagesy(¥water);
    21 imagecopy(¥back¥water¥x¥y, 0, 0, ¥w_w¥w_h);
    22 imagejpeg(¥back,./images/hee8.jpg);
    23 imagedestroy(¥back);
    24 imagedestroy(¥water);
    25 }
    26 mark_pic(./images/hee.jpg, ./images/gaolf.gif, 50, 200);


    扭转



     1 <?PHP
    
    2 /
    3 扭转
    4 edit by www.jbxue.com
    5 /
    6 ¥back=imagecreatejpeg(./images/hee.jpg);
    7
    8 ¥new=imagerotate(¥back, 45, 0);
    9 imagejpeg(¥new, ./images/hee9.jpg);
    10 ?>


    程度翻转垂直翻转



     1 <?php
    
    2 /
    3 程度翻转 垂直翻转
    4 edit by www.jbxue.com
    5 /
    6 function turn_y(¥background¥newfile){
    7
    8 ¥back=imagecreatejpeg(¥background);
    9 ¥width=imagesx(¥back);
    10 ¥height=imagesy(¥back);
    11 ¥new=imagecreatetruecolor(¥width¥height);
    12 for¥x=0; ¥x < ¥width; ¥x++){
    13 imagecopy(¥new¥back¥width-¥x-1, 0, ¥x, 0, 1, ¥height);
    14 }
    15 imagejpeg(¥new¥newfile);
    16 imagedestroy(¥back);
    17 imagedestroy(¥new);
    18 }
    19 function turn_x(¥background¥newfile){
    20 ¥back=imagecreatejpeg(¥background);
    21 ¥width=imagesx(¥back);
    22 ¥height=imagesy(¥back);
    23 ¥new=imagecreatetruecolor(¥width¥height);
    24 for¥y=0; ¥y < ¥height; ¥y++){
    25 imagecopy(¥new¥back,0, ¥height-¥y-1, 0, ¥y¥width, 1);
    26 }
    27 imagejpeg(¥new¥newfile);
    28 imagedestroy(¥back);
    29 imagedestroy(¥new);
    30 }
    31 turn_y(./images/hee.jpg, ./images/hee11.jpg);
    32 turn_x(./images/hee.jpg, ./images/hee12.jpg);
    33 ?>

    无论对感情还是对生活,“只要甜不要苦”都是任性而孩子气的,因为我们也不完美,我们也会伤害人。正因为我们都不完美,也因为生活从不是事事如意,所以对这些“瑕疵”的收纳才让我们对生活、对他人的爱变得日益真实而具体。—— 汪冰《世界再亏欠你,也要敢于拥抱幸福》
    分享到: