Home > Backend Development > PHP Tutorial > PHP Essay 1-php image processing, _PHP tutorial

PHP Essay 1-php image processing, _PHP tutorial

WBOY
Release: 2016-07-12 09:07:41
Original
1129 people have browsed it

php essay 1-php image processing,

Knowledge content of php image processing

upload_image.php:

<span><!</span><span>DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span>></span>
<span><</span><span>html </span><span>xmlns</span><span>="http://www.w3.org/1999/xhtml"</span><span>></span>
<span><</span><span>head</span><span>></span>
<span><</span><span>meta </span><span>http-equiv</span><span>="Content-Type"</span><span> content</span><span>="text/html; charset=gb2312"</span> <span>/></span>
 <span><</span><span>title</span><span>></span>图片上传页<span></</span><span>title</span><span>></span>
 <span><</span><span>style </span><span>type</span><span>="text/css"</span><span>></span><span>
.file-box</span><span>{</span><span> position</span><span>:</span><span>relative</span><span>;</span><span>width</span><span>:</span><span>340px</span><span>}</span><span> 
.txt</span><span>{</span><span> height</span><span>:</span><span>22px</span><span>;</span><span> border</span><span>:</span><span>1px solid #cdcdcd</span><span>;</span><span> width</span><span>:</span><span>200px</span><span>;</span><span>border-right</span><span>:</span><span>none</span><span>;</span><span>}</span><span> 
.btn</span><span>{</span><span> background-color</span><span>:</span><span>#FFF</span><span>;</span><span> border</span><span>:</span><span>1px solid #CDCDCD</span><span>;</span><span>height</span><span>:</span><span>26px</span><span>;</span><span> width</span><span>:</span><span>70px</span><span>;</span><span>}</span><span> 
.file</span><span>{</span><span> position</span><span>:</span><span>absolute</span><span>;</span><span> top</span><span>:</span><span>0</span><span>;</span><span> right</span><span>:</span><span>80px</span><span>;</span><span> height</span><span>:</span><span>24px</span><span>;</span><span> filter</span><span>:</span><span>alpha(opacity:0)</span><span>;</span><span>opacity</span><span>:</span><span> 0</span><span>;</span><span>width</span><span>:</span><span>260px </span><span>}</span>
 <span></</span><span>style</span><span>></span>
 <span><</span><span>script  </span><span>type</span><span>="text/javascript"</span> <span>></span>
 <span>//</span><span>JS获取路径 ---成功</span>
    <span>function</span><span> change()<br />  {
          </span><span>var</span><span> pic</span><span>=</span><span>document.getElementById(</span><span>'</span><span>pic</span><span>'</span><span>);
          </span><span>var</span><span> file</span><span>=</span><span>document.getElementById(</span><span>'</span><span>fileField</span><span>'</span><span>);
          pic.src</span><span>=</span><span>file.value;
          </span><span>if</span><span>(file.files){
            </span><span>if</span><span>(file.files.item(</span><span>0</span><span>)){
            </span><span>//</span><span>src = window.URL.createObjectURL(fileObj.files[0]);</span>
<span>            url</span><span>=</span><span>window.URL.createObjectURL(file.files.item(</span><span>0</span><span>));  
             document.getElementById(</span><span>'</span><span>textfield</span><span>'</span><span>).value </span><span>=</span><span> pic.src ;
             pic.src</span><span>=</span><span>url;
            }
          }
   } 
 </span><span></</span><span>script</span><span>></span> 
<span></</span><span>head</span><span>></span>

<span><</span><span>body</span><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><span>table </span><span>border</span><span>="1"</span><span>></span>
                <span><</span><span>tr</span><span>></span>
                    <span><</span><span>td</span><span>></span>上传人名称:<span></</span><span>td</span><span>></span>
                    <span><</span><span>td</span><span>><</span><span>input </span><span>type</span><span>="text"</span><span> class</span><span>='txt' </span><span>name</span><span>="username"</span> <span>/></</span><span>td</span><span>></span>
                <span></</span><span>tr</span><span>></span>
                <span><</span><span>tr</span><span>></span>
                    <span><</span><span>td</span><span>></span>*上传图片:<span></</span><span>td</span><span>></span>
                    <span><</span><span>td</span><span>><</span><span>div </span><span>class</span><span>="file-box"</span><span>><</span><span>input </span><span>type</span><span>='text' </span><span>name</span><span>='textfield' </span><span>id</span><span>='textfield' </span><span>class</span><span>='txt' </span><span>/><</span><span>input </span><span>type</span><span>='button' </span><span>class</span><span>='btn' </span><span>value</span><span>='浏览...' </span><span>/><</span><span>br </span><span>/></span>
                        <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="uploadfile"</span><span> class</span><span>="file"</span><span> id</span><span>="fileField"</span><span> size</span><span>="28"</span><span> onchange</span><span>="change();"</span> <span>/></span> 
                        <span><</span><span>input </span><span>type</span><span>="hidden"</span><span> id</span><span>="theFilePath"</span><span> name</span><span>="theFilePath"</span><span> value</span><span>=""</span><span>></span>
                    <span></</span><span>div</span><span>></span>
                    <span></</span><span>td</span><span>></span>
                <span></</span><span>tr</span><span>></span>
                <span><</span><span>tr</span><span>></span>
                    <span><</span><span>td </span><span>colspan</span><span>="2"</span><span>></span>
                        <span><</span><span>small</span><span>><</span><span>em</span><span>></span> * 可接受的图像格式:GIF,JPG、JPEG和PNG。上传后图片会变为1280X960大小。<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><span>tr</span><span>></span>
                    <span><</span><span>td</span><span>></span>图片标题:<span></</span><span>td</span><span>></span>
                    <span><</span><span>td</span><span>><</span><span>input </span><span>type</span><span>="text"</span><span> class</span><span>='txt' </span><span>name</span><span>="caption"</span><span>/></</span><span>td</span><span>></span>
                <span></</span><span>tr</span><span>></span>
                <span><</span><span>tr</span><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><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="submit"</span><span> value</span><span>="Upload"</span><span>  class</span><span>='btn' </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>img </span><span>src</span><span>=""</span><span>  name</span><span>="pic"</span><span> id</span><span>="pic"</span> <span>/></span> <span><</span><span>br </span><span>/></span>
        <span></</span><span>form</span><span>></span>
<span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span>
Copy after login

check_image.php Upload the collection of images, save them in MySQL database, enlarge the images to 1280X960, add text to the images, PS process the images, thumbnail the images, and add LOGO watermarks to the images. The feature set is powerful.

<?<span>php     
    </span><span>//</span><span>session</span>
    <span>session_start</span><span>();
    </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>['uploadfile']['error'<span>])
                {
                    </span><span>case</span> UPLOAD_ERR_INI_SIZE: <span>//</span><span>其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值</span>
                        <span>die</span>('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值'<span>);
                    </span><span>break</span><span>;
                    </span><span>case</span> UPLOAD_ERR_FORM_SIZE: <span>//</span><span>其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值</span>
                        <span>die</span>('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值'<span>);
                    </span><span>break</span><span>;
                    </span><span>case</span> UPLOAD_ERR_PARTIAL: <span>//</span><span>其值为 3,文件只有部分被上传</span>
                        <span>die</span>('文件只有部分被上传'<span>);
                    </span><span>break</span><span>;
                    </span><span>case</span> UPLOAD_ERR_NO_FILE: <span>//</span><span>其值为 4,没有文件被上传</span>
                        <span>die</span>('没有文件被上传'<span>);
                    </span><span>break</span><span>;
                    </span><span>case</span> UPLOAD_ERR_NO_TMP_DIR: <span>//</span><span>其值为 6,找不到临时文件夹</span>
                        <span>die</span>('找不到临时文件夹'<span>);
                    </span><span>break</span><span>;
                    </span><span>case</span> UPLOAD_ERR_CANT_WRITE: <span>//</span><span>其值为 7,文件写入失败</span>
                        <span>die</span>('文件写入失败'<span>);
                    </span><span>break</span><span>;
                    </span><span>case</span> UPLOAD_ERR_EXTENSION: <span>//</span><span>其他异常</span>
                        <span>die</span>('其他异常'<span>);
                    </span><span>break</span><span>;
                }
            }
        </span><span>//</span><span>判断图片的后缀</span>
        <span>switch</span>(<span>$type</span><span>)
        {
            </span><span>case</span> IMAGETYPE_GIF:
                <span>$ext</span> = '.gif'<span>;
            </span><span>break</span><span>;
            </span><span>case</span> IMAGETYPE_JPEG:
                <span>$ext</span> = '.jpg'<span>;
            </span><span>break</span><span>;    
            </span><span>case</span> IMAGETYPE_PNG:
                <span>$ext</span> = '.png'<span>;
            </span><span>break</span><span>;    
            </span><span>default</span>    :
                <span>die</span>('您上传的文件不支持的文件类型。'<span>);
        }
        </span><span>//</span><span>关键位置-取出文件后缀</span>
        <span>$_SESSION</span>['ext'] = <span>$ext</span><span>;
    }
    
    </span><span>/*</span><span>
    //改变图像的大小为1280x960的方法
    function resizepic($imgsrc,$imagename,$toimgwidth,$toimgheigth)
    {
        //$imgsrc:图片路径 $imgname: 图片保存的名称 $toimgwidth: 图片最终的宽度 $toimgheigth: 图片最终的高度
        $arr = getimagesize($imgsrc);
        $imgWidth =  $toimgwidth;
        $imgHeigth =  $toimgheigth;
        if($_SESSION['ext'] == '.jpg')
        {
             header('Content-Type:image/jpeg');
             $imgSrc = imagecreatefromjpeg($imgsrc);
        }
        if($_SESSION['ext'] == '.png')
        {
            header('Content-Type:image/png');
            $imgSrc = imagecreatefrompng($imgsrc);
        }
        if($_SESSION['ext'] == '.gif')
        {
            header('Content-Type:image/gif');
            $imgSrc = imagecreatefromgif($imgsrc);
        }
        
        $image = imagecreatetruecolor($imgWidth, $imgHeigth);
        imagecopyresampled($image,$imgSrc,0,0,0,0,$imgWidth,$imgHeigth,$arr[0],$arr[1]);
        if($_SESSION['ext'] == '.jpg')
        {
            imagejpeg($image,$dir.'/'.$imagename);
        }
        if($_SESSION['ext'] == '.png')
        {
            imagepng($image,$dir.'/'.$imagename);
        }
        if($_SESSION['ext'] == '.gif')
        {
            imagegif($image,$dir.'/'.$imagename);
        }
        //销毁由url生成的图片
        imagedestroy($image);
    }
    </span><span>*/</span>
    

    <span>//</span><span>创建moviesite数据库--MySQL</span>
    <span>$coon</span> = <span>mysql_connect</span>("localhost","root","lifu") or <span>die</span>('不能连接到数据库'<span>);
    </span><span>if</span> (!<span>$coon</span><span>)
      {
          </span><span>die</span>('无法连接: ' . <span>mysql_error</span><span>());
      }
    
    </span><span>if</span> (<span>mysql_query</span>("CREATE DATABASE if not exists moviesite",<span>$coon</span>))    <span>//</span><span>如果没有就创建数据库</span>
<span>      {
          </span><span>echo</span> "数据库创建成功<br>"<span>;
      }
    </span><span>else</span><span>
      {
          </span><span>echo</span> "数据库创建错误: " . <span>mysql_error</span><span>();
      }
    </span><span>//</span><span> 创建images表</span>
    <span>mysql_select_db</span>("moviesite", <span>$coon</span><span>);
    </span><span>//</span><span>如果没有就创建表</span>
    <span>$sql</span> = "<span>CREATE TABLE if not exists images                       
    (
        image_id int NOT NULL AUTO_INCREMENT, 
        PRIMARY KEY(image_id),
        image_caption varchar(15), 
        image_username varchar(15),
        image_filename varchar(15),
        image_date  datetime
    )</span>"<span>;
    
    </span><span>mysql_query</span>(<span>$sql</span>,<span>$coon</span><span>);
    </span><span>mysql_close</span>(<span>$coon</span><span>);
    
    </span><span>//</span><span>修改图片效果</span>
    <span>$db</span> = <span>mysql_connect</span>('localhost','root','lifu') or <span>die</span>('不能连接到数据库'<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:\phpStudy\WWW\loadimages\images'<span>;
    
    </span><span>//</span><span>缩略图的路径</span>
    <span>$thumbdir</span> = 'D:\phpStudy\WWW\loadimages\images\thumbs'<span>;
    
    </span><span>//</span><span>设置环境变量</span>
    <span>putenv</span>('GDFONTPATH='.'C:\Windows\Fonts'<span>);
    </span><span>$font</span> = "C:\Windows\Fonts\arial.ttf"<span>;
    
    </span><span>//</span><span>upload_image.php页面传递过来的参数,如果是上传图片</span>
    <span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>)
    {        </span><span>/*</span><span>代码重复
            if($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)
            {
                switch($_FILES['uploadfile']['error'])
                {
                    case UPLOAD_ERR_INI_SIZE: //其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值
                        die('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');
                    break;
                    case UPLOAD_ERR_FORM_SIZE: //其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值
                        die('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值');
                    break;
                    case UPLOAD_ERR_PARTIAL: //其值为 3,文件只有部分被上传
                        die('文件只有部分被上传');
                    break;
                    case UPLOAD_ERR_NO_FILE: //其值为 4,没有文件被上传
                        die('没有文件被上传');
                    break;
                    case UPLOAD_ERR_NO_TMP_DIR: //其值为 6,找不到临时文件夹
                        die('找不到临时文件夹');
                    break;
                    case UPLOAD_ERR_CANT_WRITE: //其值为 7,文件写入失败
                        die('文件写入失败');
                    break;
                    case UPLOAD_ERR_EXTENSION: //其他异常
                        die('其他异常');
                    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>/*</span><span>getimagesize方法返回一个数组,
        $width : 索引 0 包含图像宽度的像素值,
        $height : 索引 1 包含图像高度的像素值,
        $type : 索引 2 是图像类型的标记:
        = GIF,2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 
        = TIFF(intel byte order),8 = TIFF(motorola byte order),
        = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM,
        $attr : 索引 3 是文本字符串,内容为&ldquo;height="yyy" width="xxx"&rdquo;,可直接用于 IMG 标记
        </span><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>//</span><span>imagecreatefromgXXX方法从一个url路径中创建一个新的图片</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>);
                </span><span>$ext</span> = '.gif'<span>;
                </span><span>$array</span> = <span>getimagesize</span>(<span>$_FILES</span>['uploadfile']['tmp_name'<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>);
                </span><span>$ext</span> = '.jpg'<span>;
                </span><span>$array</span> = <span>getimagesize</span>(<span>$_FILES</span>['uploadfile']['tmp_name'<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>);
                </span><span>$ext</span> = '.png'<span>;
                </span><span>$array</span> = <span>getimagesize</span>(<span>$_FILES</span>['uploadfile']['tmp_name'<span>]);
            </span><span>break</span><span>;    
            </span><span>default</span>    :
                <span>die</span>('您上传的文件不支持的文件类型。'<span>);
        }
        
        
        </span><span>//</span><span>将图片改为1280X960
        //新建一个指定大小的真彩色图像big</span>
        <span>$big</span> = imagecreatetruecolor(1280, 960<span>);
        </span><span>//</span><span>重采样拷贝部分图像并调整大小为1280X960</span>
        imagecopyresampled(<span>$big</span>, <span>$image</span>, 0, 0, 0, 0, 1280, 960, <span>$array</span>[0], <span>$array</span>[1<span>]);
        
        
        </span><span>$query</span> = 'insert into images(image_caption,image_username,image_date) values ("'.<span>$image_caption</span>.'","'.<span>$image_username</span>.'",now())'<span>;
        </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>用写入的id作为图片的名字,避免同名的文件存放在同一目录中</span>
        <span>$imagename</span> = <span>$last_id</span>.<span>$ext</span><span>;
        </span><span>$image_id</span> = <span>$last_id</span><span>;
        
        </span><span>$query</span> = 'update images set image_filename="'.<span>$imagename</span>.'" where image_id='.<span>$last_id</span><span>;
        </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>//</span><span>有url指定的图片创建图片并保存到指定目录</span>
        <span>switch</span>(<span>$type</span><span>)
        {
            </span><span>case</span> IMAGETYPE_GIF:<span>
                imagegif(</span><span>$big</span>,<span>$dir</span>.'/'.<span>$imagename</span><span>);
            </span><span>break</span><span>;
            </span><span>case</span> IMAGETYPE_JPEG:<span>
                imagejpeg(</span><span>$big</span>,<span>$dir</span>.'/'.<span>$imagename</span><span>);
            </span><span>break</span><span>;
            </span><span>case</span> IMAGETYPE_PNG:<span>
                imagepng(</span><span>$big</span>,<span>$dir</span>.'/'.<span>$imagename</span><span>);
            </span><span>break</span><span>;
        }
        </span><span>//</span><span>销毁由url生成的图片</span>
        imagedestroy(<span>$big</span><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>//</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>.<span>$_SESSION</span>['ext'<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'].<span>$_SESSION</span>['ext'<span>]))
        {
            </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.jpg'<span>)
            {
                </span><span>$image</span> = imagecreatefromjpeg(<span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'<span>]);
            }
            </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.png'<span>)
            {
                </span><span>$image</span> = imagecreatefrompng(<span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'<span>]);
            }
            </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.gif'<span>)
            {
                </span><span>$image</span> = imagecreatefromgif(<span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'<span>]);
            }
        }
        </span><span>else</span><span>
        {
            </span><span>die</span>('指定的无效图像'<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>;    
        }
        
        </span><span>//</span><span>图片指定位置打印文字</span>
        <span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>]))
        {    </span><span>/*</span><span>
            array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
            $image  图像源   $size    字体大小   $angle   逆时针旋转角度   $fontfile  字体类型   $text  文本内容
            </span><span>*/</span><span>
            imagettftext(</span><span>$image</span> , 12 , 0 , 20 , 20 , 0 , <span>$font</span> , <span>$image_caption</span><span>);
        }
        
        </span><span>//</span><span>图片指定位置打LOGO</span>
        <span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_logo'<span>]))
        {
            </span><span>//</span><span>获取水印图片的尺寸并创建水印</span>
            <span>list</span>(<span>$wmk_width</span> , <span>$wmk_height</span>) = <span>getimagesize</span>('images/logo.png'<span>);
            </span><span>//</span><span>$x = ($width-$wmk_width) / 2;
            //$y = ($height-$wmk_height)/2;</span>
            <span>$x</span> = <span>$width</span>-<span>$wmk_width</span><span>;
            </span><span>$y</span> = <span>$height</span>-<span>$wmk_height</span><span>;
            </span><span>$wmk</span> = imagecreatefrompng('images/logo.png'<span>);
            </span><span>//</span><span>把水印图片和原图片合并在一起</span>
            <span>/*</span><span>
            将 wmk 图像中坐标从 0,0 开始,宽度为 $wmk_width,高度为 $wmk_height 的一部分拷贝到 image 图像中坐标为 $x 和 $y 的位置上。
            两图像将根据 pct=20 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为                     
            100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
            </span><span>*/</span><span>
            imagecopymerge(</span><span>$image</span> , <span>$wmk</span> , <span>$x</span> , <span>$y</span> , 0 , 0 , <span>$wmk_width</span> , <span>$wmk_height</span> , 20<span>);
            </span><span>//</span><span>清除水印图片</span>
            imagedestroy(<span>$wmk</span><span>);
        }
        
        </span><span>//</span><span>缩略图大小</span>
        <span>$thumb_width</span> = <span>$width</span> * 0.10<span>;
        </span><span>$thumb_height</span> = <span>$height</span> * 0.10<span>;
                       
        </span><span>//</span><span>创建一个缩略图</span>
        <span>$thumb</span> = imagecreatetruecolor(<span>$thumb_width</span>, <span>$thumb_height</span><span>);
        imagecopyresampled(</span><span>$thumb</span>, <span>$image</span>, 0, 0, 0, 0, <span>$thumb_width</span>, <span>$thumb_height</span>, <span>$width</span>, <span>$height</span><span>);
        
        </span><span>//</span><span>保存缩略图</span>
        <span>if</span>(<span>$_SESSION</span>['ext'] == '.jpg'<span>)
        {
            imagejpeg(</span><span>$thumb</span>, <span>$thumbdir</span> . '/' . <span>$_POST</span>['id'] . <span>$_SESSION</span>['ext'], 100<span>);
        }
        </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.png'<span>)
        {</span>
            imagepng(<span>$thumb</span>, <span>$thumbdir</span> . '/' . <span>$_POST</span>['id'] . <span>$_SESSION</span>['ext'<span>]);
        }
        </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.gif'<span>)
        {
            imagegif(</span><span>$thumb</span>, <span>$thumbdir</span> . '/' . <span>$_POST</span>['id'] . <span>$_SESSION</span>['ext'<span>]);
        }
        </span><span>echo</span> "修改好的缩略图:<img src='"."images/thumbs/" . <span>$_POST</span>['id'] . <span>$_SESSION</span>['ext']."' alt='' /> <br />"<span>;
        </span><span>//</span><span>清除缩略图</span>
        imagedestroy(<span>$thumb</span><span>);
        
        </span><span>//</span><span>保存原图</span>
        <span>if</span>(<span>$_SESSION</span>['ext'] == '.jpg'<span>)
        {
            imagejpeg(</span><span>$image</span> , <span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'] , 100<span>);
        }
        </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.png'<span>)
        {
            </span><span>//</span><span>压缩等级必须是0-9的png文件</span>
            imagepng(<span>$image</span> , <span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'<span>]);
        }
        </span><span>if</span>(<span>$_SESSION</span>['ext'] == '.gif'<span>)
        {
            imagegif(</span><span>$image</span> , <span>$dir</span>.'/'.<span>$_POST</span>['id'].<span>$_SESSION</span>['ext'<span>]);
        }    
        </span><span>echo</span> "修改好的大图:<img src='"."images/" . <span>$_POST</span>['id'] . <span>$_SESSION</span>['ext']."' alt='' /> <br />"<span>;
    </span>?>
        <html>
            <head>
                <title>你的图片在这!</title>
            </head>
            <body>
                <h1>你的图片被保存!</h1>
                <img src="images/<?php echo <span>$_POST</span>['id'];?>.jpg" alt="" />
            </body>
        </html>
    <?<span>php 
    }
    </span><span>else</span><span>
    {   
    </span>?>
    <html>
        <head>
            <title>你的图片在这!</title>
        </head>
        <body>
            <p>这里是你上传到服务器上的图片:</p>
        </body>
    </html>
    <?<span>php
        </span><span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>)
        {
            </span><span>$imagename</span> = 'images/'.<span>$image_id</span>.<span>$_SESSION</span>['ext'<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><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>]))
            {
                </span><span>$imagename</span> .= '&capt='.<span>urlencode</span>(<span>$image_caption</span><span>);
            }
            </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_logo'<span>]))
            {
                </span><span>$imagename</span> .= '&logo=1'<span>;
            }
        }
    </span>?>
    <div>
    <?php  <span>echo</span>  "src:".<span>$imagename</span> ;  ?>
    <img src="<?php echo <span>$imagename</span>;?>">as</span>:</td>
            <td><?php <span>echo</span>  "moviesite数据库images表第".<span>$image_id</span> ."行!" ;?></td>
        </tr>
        <tr>
            <td>Height:</td>
            <td><?php <span>echo</span> <span>$height</span>;?></td>
        </tr>
        <tr>
            <td>Widht:</td>
            <td><?php <span>echo</span> <span>$width</span>;?></td>
        </tr>
        <tr>
            <td>Upload <span>date</span>:</td>
            <td><?php <span>echo</span> <span>$image_date</span>;?></td>
        </tr>
    </table>
    <p>你可以从下面的选项列表中对你的图像应用一个特殊的效果。注:使用任何一个过滤器,保存一个图像 <em>可以撤消</em></p>
    <form action="<?php echo <span>$_SERVER</span>['PHP_SELF'];?>" method="post">
        
            <input type="hidden" name="id" value="<?php echo <span>$image_id</span>;?>"/>
            <select name="effect" id="">
                <option value="-1">保持原状</option>
                <?<span>php 
                    </span><span>echo</span> '<option value="'.IMG_FILTER_GRAYSCALE.'" '<span>;
                    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['effect']) && <span>$_POST</span>['effect'] ==<span> IMG_FILTER_GRAYSCALE)
                    {
                        </span><span>echo</span> 'selected="selected"'<span>;
                    }
                    </span><span>echo</span> ' >灰度图像</option>'<span>;
                    
                    </span><span>echo</span> '<option value="'.IMG_FILTER_GAUSSIAN_BLUR.'"'<span>;
                    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['effect']) && <span>$_POST</span>['effect'] ==<span> IMG_FILTER_GAUSSIAN_BLUR)
                    {
                        </span><span>echo</span> ' selected="selected"'<span>;
                    }
                    </span><span>echo</span> '>高斯算法模糊图像</option>'<span>;
                    
                    </span><span>echo</span> '<option value="'.IMG_FILTER_EMBOSS.'"'<span>;
                    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['effect']) && <span>$_POST</span>['effect'] ==<span> IMG_FILTER_EMBOSS)
                    {
                        </span><span>echo</span> 'selected="selected"'<span>;
                    }
                    </span><span>echo</span> '>图像浮雕化</option>'<span>;
                    
                    </span><span>echo</span> '<option value="'.IMG_FILTER_NEGATE.'"'<span>;
                    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['effect']) && <span>$_POST</span>['effect'] ==<span> IMG_FILTER_NEGATE)
                    {
                        </span><span>echo</span> 'selected="selected"'<span>;
                    }
                    </span><span>echo</span> '>图像中所有颜色反转</option>'<span>;
                </span>?>
            </select><br />   
            <?<span>php 
                </span><span>echo</span> '<input type="checkbox" name="emb_caption"'<span>;
                </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>]))
                {
                    </span><span>echo</span> ' checked="checked"'<span>;
                }
                </span><span>echo</span> ' />是否嵌入在图像的标题?'<span>;
                </span><span>echo</span> '<br />'<span>;
                
                </span><span>//</span><span>添加水印选项</span>
                <span>echo</span> '<input type="checkbox" name="emb_logo" '<span>;
                </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_logo'<span>]))
                {
                    </span><span>echo</span> 'checked="checked"'<span>;
                }
                </span><span>echo</span> ' />是否嵌入668LOGO?'. '<br />'<span>;
            </span>?>         
            <input type="submit" value="Save" name="submit" />
            <!--<input type="submit" value="Preview" name="submit" />-->
         </form>
        </div>

    <?<span>php 
    }
    </span><span>mysql_close</span>(<span>$db</span><span>);
    </span>?>
Copy after login

Originally there was a Preview, but I stopped doing it later, but the code is still uploaded image_effect.php and Preview has been commented out.

<?<span>php 
</span><span>//</span><span>上传文件的路径</span>
<span>$dir</span> = 'D:\phpStudy\WWW\loadimages\images'<span>;
</span><span>//</span><span>设置环境变量
putenv('GDFONTPATH='.'C:\Windows\Fonts');</span>
<span>$font</span> = "C:\Windows\Fonts\arial.ttf"<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'].<span>$_SESSION</span>['ext'<span>]))
{
    </span><span>$image</span> = imagecreatefromjpeg(<span>$dir</span>.'/'.<span>$_GET</span>['id'].<span>$_SESSION</span>['ext'<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>if</span>(<span>isset</span>(<span>$_GET</span>['capt'<span>]))
{
    </span><span>//</span><span>echo $_GET['capt'];</span>
    imagettftext(<span>$image</span>, 12, 0, 20, 20, 0, <span>$font</span>, <span>$_GET</span>['capt'<span>]);
}

</span><span>if</span>(<span>isset</span>(<span>$_GET</span>['logo'<span>]))
{
    </span><span>list</span>(<span>$widht</span> , <span>$height</span>) = <span>getimagesize</span>(<span>$dir</span>.'/'.<span>$_GET</span>['id'].<span>$_SESSION</span>['ext'<span>]);
    </span><span>list</span>(<span>$wmk_width</span> , <span>$wmk_height</span>) = <span>getimagesize</span>('images/logo.png'<span>);
    </span><span>$x</span> = <span>$width</span>-<span>$wmk_width</span><span>;
    </span><span>$y</span> = <span>$height</span>-<span>$wmk_height</span><span>;
    </span><span>$wmk</span> = imagecreatefrompng('images/logo.png'<span>);
    imagecopymerge(</span><span>$image</span> , <span>$wmk</span> , <span>$x</span> , <span>$y</span> , 0 , 0 , <span>$wmk_width</span> , <span>$wmk_height</span> , 20<span>);
    imagedestroy(</span><span>$wmk</span><span>);
}


</span><span>if</span>(<span>$_SESSION</span>['ext'] == '.jpg'<span>)
{
     </span><span>header</span>('Content-Type:image/jpeg'<span>);
     imagejpeg(</span><span>$image</span> , '' , 100<span>);
}
</span><span>if</span>(<span>$_SESSION</span>['ext'] == '.png'<span>)
{
    </span><span>header</span>('Content-Type:image/png'<span>);
    imagepng(</span><span>$image</span> , ''<span> );
}
</span><span>if</span>(<span>$_SESSION</span>['ext'] == '.gif'<span>)
{
    </span><span>header</span>('Content-Type:image/gif'<span>);
    imagegif(</span><span>$image</span> , ''<span> );
}    

</span>?>
Copy after login

Content summary:

I am self-taught in PHP. There are many irregularities in code writing, and the code in some positions is repetitive and cumbersome. It needs to be strengthened in the future.

upload_image.php: Mainly HTML Javascript Css. There is nothing to say. The main thing is javascript calling window.URL.createObjectURL(fileObj) method. Get the relative path of the image and display the image.

check_image.php: 1. Session is used to save the suffix name of the image passed from upload_image.php.

2. Creation of database moviesite and creation of images table

    <span>$sql</span> = "<span>CREATE TABLE if not exists images                       
    (
        image_id int NOT NULL AUTO_INCREMENT, 
        PRIMARY KEY(image_id),
        image_caption varchar(15), 
        image_username varchar(15),
        image_filename varchar(15),
        image_date  datetime
    )</span>";
Copy after login

3. Use imagecreatetruecolor and imagecopyresampled to adjust the image size to 1280X960. Then upload and save the image to the specified location.

<span>     //</span><span>将图片改为1280X960
        //新建一个指定大小的真彩色图像big</span>
        <span>$big</span> = imagecreatetruecolor(1280, 960<span>);
        </span><span>//</span><span>重采样拷贝部分图像并调整大小为1280X960</span>
        imagecopyresampled(<span>$big</span>, <span>$image</span>, 0, 0, 0, 0, 1280, 960, <span>$array</span>[0], <span>$array</span>[1]);
Copy after login

4. If the image has been uploaded, get the image name from the database

        <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>//</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>.<span>$_SESSION</span>['ext']);
Copy after login

5. bool imagefilter ( resource $src_im , int $filtertype [, int $arg1 [, int $arg2 [, int $arg3 ]]] ) Use filters on images PS pictures

<span> imagefilter() 把过滤器 filtertype 应用到图像上,在需要时使用 arg1,arg2 和 arg3。

filtertype 可以是下列中的一个:

    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 设定柔滑级别。</span>
Copy after login

6. Add text to pictures

        <span>//</span><span>图片指定位置打印文字</span>
        <span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>]))
        {    </span><span>/*</span><span>
            array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
            $image  图像源   $size    字体大小   $angle   逆时针旋转角度   $fontfile  字体类型   $text  文本内容
            </span><span>*/</span><span>
            imagettftext(</span><span>$image</span> , 12 , 0 , 20 , 20 , 0 , <span>$font</span> , <span>$image_caption</span><span>);
        }</span>
Copy after login

7. Logo the picture

        <span>//</span><span>图片指定位置打LOGO</span>
        <span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_logo'<span>]))
        {
            </span><span>//</span><span>获取水印图片的尺寸并创建水印</span>
            <span>list</span>(<span>$wmk_width</span> , <span>$wmk_height</span>) = <span>getimagesize</span>('images/logo.png'<span>);
            </span><span>//</span><span>$x = ($width-$wmk_width) / 2;
            //$y = ($height-$wmk_height)/2;</span>
            <span>$x</span> = <span>$width</span>-<span>$wmk_width</span><span>;
            </span><span>$y</span> = <span>$height</span>-<span>$wmk_height</span><span>;
            </span><span>$wmk</span> = imagecreatefrompng('images/logo.png'<span>);
            </span><span>//</span><span>把水印图片和原图片合并在一起</span>
            <span>/*</span><span>
            将 wmk 图像中坐标从 0,0 开始,宽度为 $wmk_width,高度为 $wmk_height 的一部分拷贝到 image 图像中坐标为 $x 和 $y 的位置上。
            两图像将根据 pct=20 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为                     
            100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
            </span><span>*/</span><span>
            imagecopymerge(</span><span>$image</span> , <span>$wmk</span> , <span>$x</span> , <span>$y</span> , 0 , 0 , <span>$wmk_width</span> , <span>$wmk_height</span> , 20<span>);
            </span><span>//</span><span>清除水印图片</span>
            imagedestroy(<span>$wmk</span><span>);
        }</span>
Copy after login

8. Thumbnail 128X96 size

        <span>//</span><span>缩略图大小</span>
        <span>$thumb_width</span> = <span>$width</span> * 0.10<span>;
        </span><span>$thumb_height</span> = <span>$height</span> * 0.10<span>;
                       
        </span><span>//</span><span>创建一个缩略图</span>
        <span>$thumb</span> = imagecreatetruecolor(<span>$thumb_width</span>, <span>$thumb_height</span><span>);
        imagecopyresampled(</span><span>$thumb</span>, <span>$image</span>, 0, 0, 0, 0, <span>$thumb_width</span>, <span>$thumb_height</span>, <span>$width</span>, <span>$height</span>);
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1059472.htmlTechArticlephp essay 1-php image processing, knowledge content of php image processing upload_image.php: ! DOCTYPE html PUBLIC "- //W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template