Heim  >  Artikel  >  php教程  >  PHP修改图片

PHP修改图片

WBOY
WBOYOriginal
2016-06-06 19:47:071535Durchsuche

这篇是关于修改图片的效果,主要还是用到php中的GD库中的函数,没想到php还有这凶残能力,出乎我的预料。 先看代码upload_image.php,主要是一个上传控件,用来选择图片 html head title / title style type ="text/css" / style / head body form action ="

这篇是关于修改图片的效果,主要还是用到php中的GD库中的函数,没想到php还有这凶残能力,出乎我的预料。

先看代码upload_image.php,主要是一个上传控件,用来选择图片

<span><span>html</span><span>></span>
    <span><span>head</span><span>></span>
        <span><span>title</span><span>></span><span>title</span><span>></span>
        <span><span>style </span><span>type</span><span>="text/css"</span><span>></span><span>style</span><span>></span>
    <span></span><span>head</span><span>></span>
    <span><span>body</span><span>></span>
        <span><span>form </span><span>action</span><span>="check_image.php"</span><span> method</span><span>="post"</span><span> enctype</span><span>="multipart/form-data"</span><span>></span>
            <span><span>table</span><span>></span>
                <span><span>tr</span><span>></span>
                    <span><span>td</span><span>></span>Your username<span></span><span>td</span><span>></span>
                    <span><span>td</span><span>><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="username"</span> <span>/></span><span>td</span><span>></span>
                <span></span><span>tr</span><span>></span>
                <span><span>tr</span><span>></span>
                    <span><span>td</span><span>></span>Upload image*<span></span><span>td</span><span>></span>
                    <span><span>td</span><span>><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="uploadfile"</span><span>/></span><span>td</span><span>></span>
                <span></span><span>tr</span><span>></span>
                <span><span>tr</span><span>></span>
                    <span><span>td </span><span>colspan</span><span>="2"</span><span>></span>
                        <span><span>small</span><span>><span>em</span><span>></span> * Acceptable image formats include: GIF, JPG/JPEG and PNG.<span></span><span>em</span><span>></span><span>small</span><span>></span>
                    <span></span><span>td</span><span>></span>
                <span></span><span>tr</span><span>></span>
                <span><span>tr</span><span>></span>
                    <span><span>td</span><span>></span>Image Caption<span></span><span>td</span><span>></span>
                    <span><span>td</span><span>><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="caption"</span><span>/></span><span>td</span><span>></span>
                <span></span><span>tr</span><span>></span>
                <span><span>tr</span><span>></span>
                    <span><span>td </span><span>colspan</span><span>="2"</span><span> style</span><span>="text-align:center;"</span><span>></span>
                        <span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="submit"</span><span> value</span><span>="Upload"</span> <span>/></span>
                    <span></span><span>td</span><span>></span>
                <span></span><span>tr</span><span>></span>
            <span></span><span>table</span><span>></span>
        <span></span><span>form</span><span>></span>
    <span></span><span>body</span><span>></span>
<span></span><span>html</span><span>></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

然后是上传和处理图片的逻辑check_image.php

<span>php 
</span><span>//</span><span>修改图片效果</span>
<span>$db</span> = <span>mysql_connect</span>('localhost','root','Ctrip07185419') or <span>die</span>('can not connect to database'<span>);
</span><span>mysql_select_db</span>('moviesite',<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));
</span><span>//</span><span>上传文件的路径</span>
<span>$dir</span> = 'D:\Serious\phpdev\test\images'<span>;

</span><span>//</span><span>upload_image.php页面传递过来的参数,如果是上传图片</span>
<span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>)
{
    </span><span>if</span>(<span>$_FILES</span>['uploadfile']['error'] !=<span> UPLOAD_ERR_OK)
    {
        </span><span>switch</span>(<span>$_FILES</span>['uploadfiel']['error'<span>])
        {
            </span><span>case</span> UPLOAD_ERR_INI_SIZE:
                <span>die</span>('The uploaded file exceeds the upload_max_filesize directive'<span>);
            </span><span>break</span><span>;
            </span><span>case</span> UPLOAD_ERR_FORM_SIZE:
                <span>die</span>('The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'<span>);
            </span><span>break</span><span>;
            </span><span>case</span> UPLOAD_ERR_PARTIAL:
                <span>die</span>('The uploaded file was only partially uploaded'<span>);
            </span><span>break</span><span>;
            </span><span>case</span> UPLOAD_ERR_NO_FILE:
                <span>die</span>('No file was uploaded'<span>);
            </span><span>break</span><span>;
            </span><span>case</span> UPLOAD_ERR_NO_TMP_DIR:
                <span>die</span>('The server is missing a temporary folder'<span>);
            </span><span>break</span><span>;    
            </span><span>case</span> UPLOAD_ERR_CANT_WRITE:
                <span>die</span>('The server fail to write the uploaded file to the disk'<span>);
            </span><span>break</span><span>;        
            </span><span>case</span> UPLOAD_ERR_EXTENSION:
                <span>die</span>('The upload stopped by extension'<span>);
            </span><span>break</span><span>;                
        }
    }
    </span><span>$image_caption</span> = <span>$_POST</span>['caption'<span>];
    </span><span>$image_username</span> = <span>$_POST</span>['username'<span>];
    </span><span>$image_date</span> = <span>date</span>('Y-m-d'<span>);
    </span><span>list</span>(<span>$width</span>,<span>$height</span>,<span>$type</span>,<span>$attr</span>) = <span>getimagesize</span>(<span>$_FILES</span>['uploadfile']['tmp_name'<span>]);
    </span><span>$error</span> = 'The file you upload is not a supported filetype'<span>;
    </span><span>switch</span>(<span>$type</span><span>)
    {
        </span><span>case</span> IMAGETYPE_GIF:
            <span>$image</span> = imagecreatefromgif(<span>$_FILES</span>['uploadfile']['tmp_name']) or <span>die</span>(<span>$error</span><span>);
        </span><span>break</span><span>;
        </span><span>case</span> IMAGETYPE_JPEG:
            <span>$image</span> = imagecreatefromjpeg(<span>$_FILES</span>['uploadfile']['tmp_name']) or <span>die</span>(<span>$error</span><span>);
        </span><span>break</span><span>;
        </span><span>case</span> IMAGETYPE_PNG:
            <span>$image</span> = imagecreatefrompng(<span>$_FILES</span>['uploadfile']['tmp_name']) or <span>die</span>(<span>$error</span><span>);
        </span><span>break</span><span>;
        </span><span>default</span>:
        <span>break</span><span>;
    }
    </span><span>$query</span> = 'insert into images(image_caption,image_username,image_date) values("'.<span>$image_caption</span>.'" , "'.<span>$image_username</span>.'","'.<span>$image_date</span>.'")'<span>;
    </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span>,<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));
    </span><span>$last_id</span> = <span>mysql_insert_id</span><span>();
    
    </span><span>//</span><span> $imagename = $last_id.'.jpg';
    // imagejpeg($image,$dir.'/'.$imagename);
    // imagedestroy($image);</span>
    
    <span>$image_id</span> = <span>$last_id</span><span>;
    imagejpeg(</span><span>$image</span> , <span>$dir</span>.'/'.<span>$image_id</span>.'.jpg'<span>);
    imagedestroy(</span><span>$image</span><span>);
}
</span><span>else</span>  <span>//</span><span>如果图片已经上传,则从数据库中取图片名字</span>
<span>{
    </span><span>$query</span> = 'select image_id,image_caption,image_username,image_date from images where image_id='.<span>$_POST</span>['id'<span>];
    </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span>,<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));
    </span><span>extract</span>(<span>mysql_fetch_assoc</span>(<span>$result</span><span>));
    </span><span>list</span>(<span>$width</span>,<span>$height</span>,<span>$type</span>,<span>$attr</span>) = <span>getimagesize</span>(<span>$dir</span>.'/'.<span>$image_id</span>.'.jpg'<span>);
}

</span><span>//</span><span>如果是保存图片</span>
<span>if</span>(<span>$_POST</span>['submit'] == 'Save'<span>)
{
    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['id']) && <span>ctype_digit</span>(<span>$_POST</span>['id']) && <span>file_exists</span>(<span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg'<span>))
    {
        </span><span>$image</span> = imagecreatefromjpeg(<span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg'<span>);
    }
    </span><span>else</span><span>
    {
        </span><span>die</span>('invalid image specified'<span>);
    }
    </span><span>$effect</span> = (<span>isset</span>(<span>$_POST</span>['effect'])) ? <span>$_POST</span>['effect'] : -1<span>;
    </span><span>switch</span>(<span>$effect</span><span>)
    {
        </span><span>case</span> IMG_FILTER_NEGATE:<span>
            imagefilter(</span><span>$image</span> , IMG_FILTER_NEGATE);     <span>//</span><span>将图像中所有颜色反转</span>
        <span>break</span><span>;
        </span><span>case</span> IMG_FILTER_GRAYSCALE:<span>
            imagefilter(</span><span>$image</span> , IMG_FILTER_GRAYSCALE);  <span>//</span><span>将图像转换为灰度的</span>
        <span>break</span><span>;
        </span><span>case</span> IMG_FILTER_EMBOSS:<span>
            imagefilter(</span><span>$image</span> , IMG_FILTER_EMBOSS);     <span>//</span><span>使图像浮雕化</span>
        <span>break</span><span>;
        </span><span>case</span> IMG_FILTER_GAUSSIAN_BLUR:<span>
            imagefilter(</span><span>$image</span> , IMG_FILTER_GAUSSIAN_BLUR); <span>//</span><span>用高斯算法模糊图像</span>
        <span>break</span><span>;    
    }
    imagejpeg(</span><span>$image</span> , <span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg' , 100<span>);
    </span>?>
    
        
            <title>Here is your pic!</title>
        
        
            <h1>Your image has been saved!</h1>
            <img src="images/<?php%20echo%20<span>%24_POST</span>%5B'id'%5D;?>.jpg" alt="">
        
    
<span>php 
}
</span><span>else</span><span>
{
</span>?>
    
        
            <title>Here is your pic!</title>
        
        
            <h1>So how does it feel to be famous?</h1>
            <p>Here is the picture you just uploaded to your servers:</p>
            <!--<img  src="images/<?php echo <span alt="PHP修改图片" >$imagename</span>;?>" alt=""   style="max-width:90%" />-->
        
    
    <span>php
        </span><span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>)
        {
            </span><span>$imagename</span> = 'images/'.<span>$image_id</span>.'.jpg'<span>;
        }
        </span><span>else</span><span>
        {
            </span><span>$imagename</span> = 'image_effect.php?id='.<span>$image_id</span>.'&e='.<span>$_POST</span>['effect'<span>];
        }
    </span>?>
    <img src="<?php%20echo%20<span>%24imagename</span>;?>"   style="max-width:90%" alt="">
    
Image save as: $image_id?>
Height: echo $height;?>
Widht: echo $width;?>
Upload date: echo $image_date;?>

You may apply a special effect to your image from the list of option below. Note:saving an image with any of the filters applied can be undone



php } ?>

最后是一个预览效果的页面image_effect.php

<span>php 
</span><span>$dir</span> = 'D:\Serious\phpdev\test\images'<span>;

</span><span>if</span>(<span>isset</span>(<span>$_GET</span>['id']) && <span>ctype_digit</span>(<span>$_GET</span>['id']) && <span>file_exists</span>(<span>$dir</span>.'/'.<span>$_GET</span>['id'].'.jpg'<span>))
{
    </span><span>$image</span> = imagecreatefromjpeg(<span>$dir</span>.'/'.<span>$_GET</span>['id'].'.jpg'<span>);
}
</span><span>else</span><span>
{
    </span><span>die</span>('invalid image specified'<span>);
}

</span><span>$effect</span> = (<span>isset</span>(<span>$_GET</span>['e'])) ? <span>$_GET</span>['e'] : -1<span>;

</span><span>switch</span>(<span>$effect</span><span>)
{
    </span><span>case</span> IMG_FILTER_NEGATE:<span>
        imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_NEGATE);
    </span><span>break</span><span>;
    </span><span>case</span> IMG_FILTER_GRAYSCALE:<span>
        imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_GRAYSCALE);
    </span><span>break</span><span>;    
    </span><span>case</span> IMG_FILTER_EMBOSS:<span>
        imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_EMBOSS);
    </span><span>break</span><span>;    
    </span><span>case</span> IMG_FILTER_GAUSSIAN_BLUR:<span>
        imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_GAUSSIAN_BLUR);
    </span><span>break</span><span>;    
}
</span><span>header</span>('Content-Type:image/jpeg'<span>);
imagejpeg(</span><span>$image</span> , '' , 100<span>);

</span>?>

第二个image_check.php有点乱,在这个页面中有上传图片,处理图片,还有预览图片的部分逻辑,注意下面这段

    <span>php
        </span><span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>)
        {
            </span><span>$imagename</span> = 'images/'.<span>$image_id</span>.'.jpg'<span>;
        }
        </span><span>else</span><span>
        {
            </span><span>$imagename</span> = 'image_effect.php?id='.<span>$image_id</span>.'&e='.<span>$_POST</span>['effect'<span>];
        }
    </span>?>
    <img src="<?php%20echo%20<span>%24imagename</span>;?>"   style="max-width:90%" alt="">

如果是上传直接访问图片,如果是预览则从image_effect.php中读取图片,这里是从内存中读取图片并根据选择的处理效果来展示图片。如下

<span>switch</span>(<span>$effect</span><span>)
{
    </span><span>case</span> IMG_FILTER_NEGATE:<span>
        imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_NEGATE);
    </span><span>break</span><span>;
    </span><span>case</span> IMG_FILTER_GRAYSCALE:<span>
        imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_GRAYSCALE);
    </span><span>break</span><span>;    
    </span><span>case</span> IMG_FILTER_EMBOSS:<span>
        imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_EMBOSS);
    </span><span>break</span><span>;    
    </span><span>case</span> IMG_FILTER_GAUSSIAN_BLUR:<span>
        imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_GAUSSIAN_BLUR);
    </span><span>break</span><span>;    
}</span>

当使用imagefilter方法处理图片之后会把图片输出到页面,这里要注意imagejpeg方法的第二个参数是空字符串,这样它就不会写入到硬盘中了,如果第二个参数设置了会覆盖原有的图片,这样可以让用户在保存图片之前随意的预览效果,如下:

<span>header</span>('Content-Type:image/jpeg'<span>);
imagejpeg(</span><span>$image</span> , '' , 100);

在check_image.php中有调用到类似的方法,但是这里指定了第二个参数,就是用来保存图片的:

imagejpeg(<span>$image</span> , <span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg' , 100);

注意这里哦我们只写了三种处理效果,这个只是所有枚举中的一部分,我们来看所有的处理方式:

IMG_FILTER_NEGATE:将图像中所有颜色反转。
IMG_FILTER_GRAYSCALE:将图像转换为灰度的。
IMG_FILTER_BRIGHTNESS:改变图像的亮度。用 arg1 设定亮度级别。
IMG_FILTER_CONTRAST:改变图像的对比度。用 arg1 设定对比度级别。
IMG_FILTER_COLORIZE:与 IMG_FILTER_GRAYSCALE 类似,不过可以指定颜色。用 arg1,arg2 和 arg3 分别指定 red,blue 和 green。每种颜色范围是 0 到 255。
IMG_FILTER_EDGEDETECT:用边缘检测来突出图像的边缘。
IMG_FILTER_EMBOSS:使图像浮雕化。
IMG_FILTER_GAUSSIAN_BLUR:用高斯算法模糊图像。
IMG_FILTER_SELECTIVE_BLUR:模糊图像。
IMG_FILTER_MEAN_REMOVAL:用平均移除法来达到轮廓效果。
IMG_FILTER_SMOOTH:使图像更柔滑。用 arg1 设定柔滑级别。

是不是很惊艳,php很强大的。

 

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