zoukankan      html  css  js  c++  java
  • PHP生成缩略图功能

    直接上代码:

    $source_pic = @$_POST['source_pic']; //源图像
        if (!empty($source_pic))
        {
            $filename = ROOT_PATH.$source_pic;
            $pathRes = pathinfo($filename);
            $newname = "thumb_".substr($pathRes['filename'],strpos($pathRes['filename'],"_")).".".$pathRes['extension'];//生成新图像名称
            $file_ext = $pathRes['extension'];
            //header('Content-type: image/jpeg');
            // Get new dimensions
            list($width, $height) = getimagesize($filename);
            $new_width = $_POST['x2']-$_POST['x1']; //x2,x1 分别为横轴截取的两个点
            $new_height = $_POST['y2']-$_POST['y1']; //y2,y1 分别为纵轴截取的两个点
            
            // Resample
            $image_p = imagecreatetruecolor($new_width, $new_height);
            if ($file_ext=="jpg" || $file_ext=="jpeg") //生成jpg图像
            {
                $image = imagecreatefromjpeg($filename);
                imagecopyresampled($image_p, $image, 0, 0, $_POST['x1'], $_POST['y1'], $new_width, $new_height, $new_width, $new_height);
                // Output
                imagejpeg($image_p,ROOT_PATH."images/user_face/".$newname, 100);
            }
            elseif ($file_ext=="png") //生成png图像
            {
                $image = imagecreatefrompng($filename);
                imagecopyresampled($image_p, $image, 0, 0, $_POST['x1'], $_POST['y1'], $new_width, $new_height, $new_width, $new_height);
                // Output
                imagepng($image_p,ROOT_PATH."images/user_face/".$newname);
            }
            elseif ($file_ext=="gif") //生成gif图像
            {
                $image = imagecreatefromgif($filename);
                imagecopyresampled($image_p, $image, 0, 0, $_POST['x1'], $_POST['y1'], $new_width, $new_height, $new_width, $new_height);
                // Output
                imagegif($image_p,ROOT_PATH."images/user_face/".$newname);
            }
            
            $res  = $userInfoObj->updateItem($uid,array('user_face'=>$newname));
            echo "OK";
            exit();

    其他说明:选择四个点的时候,可以使用js插件进行选择 具体的链接地址为:http://files.cnblogs.com/ypeih/imagecut_demo.rar 点击下载

    注意:生成缩缩图错误的实例

    在做的过程中,突然旁边的同事使用这个插件怎么上传都出错,可是上传其他的图片文件都没有问题,代码也没有更改过,这个事情折腾了我一个多小时,最后找资料才知道是文件类型被同事更改了

    本来是gif的文件,他把后缀更改为了jpg

  • 相关阅读:
    服务器控件Repeater
    HeidiSQL无法连接Myql
    未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序搜索
    DISTINCT删除重复数据
    Makedown文件保存问题
    Makedown 本地图片问题
    vsftpd配置文件详解[转载]
    centos 本地yum源的搭建
    OpenOffice菜单乱码解决和linux下安装字体
    让secureCRT正确显示中文
  • 原文地址:https://www.cnblogs.com/ypeih/p/3280788.html
Copyright © 2011-2022 走看看