侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

php网站怎么修改图片

php  /  管理员 发布于 7年前   343

主要还是用到php中的GD库中的函数

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

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

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

<?php //修改图片效果$db = mysql_connect('localhost','root','Ctrip07185419') or die('can not connect to database');mysql_select_db('moviesite',$db) or die(mysql_error($db));//上传文件的路径$dir = 'D:\Serious\phpdev\test\images';//upload_image.php页面传递过来的参数,如果是上传图片if($_POST['submit'] == 'Upload'){    if($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK)    {        switch($_FILES['uploadfiel']['error'])        {case UPLOAD_ERR_INI_SIZE:    die('The uploaded file exceeds the upload_max_filesize directive');break;case UPLOAD_ERR_FORM_SIZE:    die('The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');break;case UPLOAD_ERR_PARTIAL:    die('The uploaded file was only partially uploaded');break;case UPLOAD_ERR_NO_FILE:    die('No file was uploaded');break;case UPLOAD_ERR_NO_TMP_DIR:    die('The server is missing a temporary folder');break;    case UPLOAD_ERR_CANT_WRITE:    die('The server fail to write the uploaded file to the disk');break;        case UPLOAD_ERR_EXTENSION:    die('The upload stopped by extension');break;}    }    $image_caption = $_POST['caption'];    $image_username = $_POST['username'];    $image_date = date('Y-m-d');    list($width,$height,$type,$attr) = getimagesize($_FILES['uploadfile']['tmp_name']);    $error = 'The file you upload is not a supported filetype';    switch($type)    {        case IMAGETYPE_GIF:$image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or die($error);        break;        case IMAGETYPE_JPEG:$image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or die($error);        break;        case IMAGETYPE_PNG:$image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or die($error);        break;        default:        break;    }    $query = 'insert into images(image_caption,image_username,image_date) values("'.$image_caption.'" , "'.$image_username.'","'.$image_date.'")';    $result = mysql_query($query,$db) or die(mysql_error($db));    $last_id = mysql_insert_id();        // $imagename = $last_id.'.jpg';    // imagejpeg($image,$dir.'/'.$imagename);    // imagedestroy($image);        $image_id = $last_id;    imagejpeg($image , $dir.'/'.$image_id.'.jpg');    imagedestroy($image);}else  //如果图片已经上传,则从数据库中取图片名字{    $query = 'select image_id,image_caption,image_username,image_date from images where image_id='.$_POST['id'];    $result = mysql_query($query,$db) or die(mysql_error($db));    extract(mysql_fetch_assoc($result));    list($width,$height,$type,$attr) = getimagesize($dir.'/'.$image_id.'.jpg');}//如果是保存图片if($_POST['submit'] == 'Save'){    if(isset($_POST['id']) && ctype_digit($_POST['id']) && file_exists($dir.'/'.$_POST['id'].'.jpg'))    {        $image = imagecreatefromjpeg($dir.'/'.$_POST['id'].'.jpg');    }    else    {        die('invalid image specified');    }    $effect = (isset($_POST['effect'])) ? $_POST['effect'] : -1;    switch($effect)    {        case IMG_FILTER_NEGATE:imagefilter($image , IMG_FILTER_NEGATE);     //将图像中所有颜色反转        break;        case IMG_FILTER_GRAYSCALE:imagefilter($image , IMG_FILTER_GRAYSCALE);  //将图像转换为灰度的        break;        case IMG_FILTER_EMBOSS:imagefilter($image , IMG_FILTER_EMBOSS);     //使图像浮雕化        break;        case IMG_FILTER_GAUSSIAN_BLUR:imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR); //用高斯算法模糊图像        break;        }    imagejpeg($image , $dir.'/'.$_POST['id'].'.jpg' , 100);    ?>    <html>        <head><title>Here is your pic!</title>        </head>        <body><h1>Your image has been saved!</h1><img src="images/<?php echo $_POST['id'];?>.jpg" alt="" />        </body>    </html><?php }else{?>    <html>        <head><title>Here is your pic!</title>        </head>        <body><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 $imagename;?>" alt="" style="float:left;" />-->        </body>    </html>    <?php        if($_POST['submit'] == 'Upload')        {$imagename = 'images/'.$image_id.'.jpg';        }        else        {$imagename = 'image_effect.php?id='.$image_id.'&e='.$_POST['effect'];        }    ?>    <img src="<?php echo $imagename;?>" style="float:left;" alt="" />    <table>        <tr><td>Image save as:</td><td><?php $image_id?></td>        </tr>        <tr><td>Height:</td><td><?php echo $height;?></td>        </tr>        <tr><td>Widht:</td><td><?php echo $width;?></td>        </tr>        <tr><td>Upload date:</td><td><?php echo $image_date;?></td>        </tr>    </table>    <p>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 <em>can be undone</em>    </p>    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">        <div><input type="hidden" name="id" value="<?php echo $image_id;?>"/><select name="effect" id="">    <option value="-1">None</option>    <?php         echo '<option value="'.IMG_FILTER_GRAYSCALE.'" ';        if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_GRAYSCALE)        {echo 'selected="selected"';        }        echo ' >Black and white</option>';    echo '<option value="'.IMG_FILTER_GAUSSIAN_BLUR.'"';        if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_GAUSSIAN_BLUR)        {echo ' selected="selected"';        }        echo '>Blur</option>';    echo '<option value="'.IMG_FILTER_EMBOSS.'"';        if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_EMBOSS)        {echo 'selected="selected"';        }        echo '>Emboss</option>';    echo '<option value="'.IMG_FILTER_NEGATE.'"';        if(isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_NEGATE)        {echo 'selected="selected"';        }        echo '>Negative</option>';    ?></select><br /><input type="submit" value="Preview" name="submit" /><br /><input type="submit" value="Save" name="submit" />        </div>    </form><?php }?>

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

<?php $dir = 'D:\Serious\phpdev\test\images';if(isset($_GET['id']) && ctype_digit($_GET['id']) && file_exists($dir.'/'.$_GET['id'].'.jpg')){    $image = imagecreatefromjpeg($dir.'/'.$_GET['id'].'.jpg');}else{    die('invalid image specified');}$effect = (isset($_GET['e'])) ? $_GET['e'] : -1;switch($effect){    case IMG_FILTER_NEGATE:        imagefilter($image , IMG_FILTER_NEGATE);    break;    case IMG_FILTER_GRAYSCALE:        imagefilter($image , IMG_FILTER_GRAYSCALE);    break;        case IMG_FILTER_EMBOSS:        imagefilter($image , IMG_FILTER_EMBOSS);    break;        case IMG_FILTER_GAUSSIAN_BLUR:        imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR);    break;    }header('Content-Type:image/jpeg');imagejpeg($image , '' , 100);?>

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

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

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

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

php中处理图片的方法:

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网站怎么修改图片的详细内容,更多请关注其它相关文章!


  • 上一条:
    php要怎么get数组
    下一条:
    php防sql注入原理
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客