【问题标题】:Upload, resize, and crop center of image with PHP使用 PHP 上传、调整大小和裁剪图像中心
【发布时间】:2011-09-23 22:04:18
【问题描述】:

我想创建一个非常基本的上传、调整大小和裁剪 PHP 脚本。 其功能将与 Twitter 用于上传头像图片的方法相同(我最后检查过)。

我希望脚本拍摄任何尺寸的图像,将最短边调整为 116 像素,然后裁剪顶部和底部(如果是横向,则裁剪左右边)以获得 116 像素乘 116 像素的正方形。

我不想要一个带有客户端调整大小或任何东西的臃肿 PHP 脚本,只需要一个简单的 PHP 调整大小和裁剪。这是怎么做到的?

【问题讨论】:

标签: php image upload resize crop


【解决方案1】:

GD 库是一个很好的起点。

http://www.php.net/manual/en/book.image.php

【讨论】:

    【解决方案2】:

    有一个简单易用的开源库,名为PHP Image Magician。它使用 GD,但将其使用简化为 3 行。

    基础用法示例:

    $magicianObj = new imageLib('racecar.jpg');
    $magicianObj -> resizeImage(100, 200, 'crop');
    $magicianObj -> saveImage('racecar_small.png');
    

    【讨论】:

    • 迄今为止最好的。许多非常易于使用的功能。谢谢!!
    【解决方案3】:

    如果你想从我上传的示例中工作,调整大小和裁剪类会完成所有这些以及其他一些很酷的东西 - 如果需要,你可以全部使用它,或者只是取出你喜欢的部分:

    http://www.mjdigital.co.uk/blog/php-upload-and-resize-image-class/

    我觉得不会太臃肿! - 你可以这样做(未经测试):

    if((isset($_FILES['file']['error']))&&($_FILES['file']['error']==0)){ // 如果文件已发布,则上传 包括('INCLUDE_CLASS_FILE_HERE.php'); $myImage = 新 _image; // 上传图片 $myImage->uploadTo = '上传/'; // 在此处设置上传文件夹 $myImage->returnType = '数组'; // 返回图像细节数组 $img = $myImage->upload($_FILES['file']); 如果($img){ $myImage->newWidth = 116; $myImage->newHeight = 116; $i = $myImage->resize(); // 调整大小为 116px 保持纵横比 // 获取新的图像高度 $imgWidth = $i['width']; // 获取新的图像宽度 $imgHeight = $i['身高']; 如果($i){ // 确定在哪里裁剪 $cropX = ($imgWidth>116) ? (($imgWidth-116)/2) : 0; $cropY = ($imgHeight>116) ? (($imgHeight-116)/2) : 0; $cropped = $myImage->crop(116,116,$cropX,$cropY); if($cropped) { echo '它成功了(我认为!)'; print_r($cropped); } else { echo '作物失败'; } } else { echo '调整大小失败'; } } else { echo '上传失败'; }

    【讨论】:

    • 谢谢 - 虽然我在原来的帖子中意识到我遗漏了裁剪方法的最后两个参数 - 哦!我现在已经在上面修复了它,所以希望它应该没问题 - 以防万一你需要使用以下方法调用裁剪方法:$myImage->crop($cropToWidth,$cropToHeight,$cropFromX,$cropFromY);
    【解决方案4】:

    我制作了这个简单易用的功能,它允许您将图像调整大小、裁剪和居中到特定的宽度和高度,它可以支持 JPG、PNG 和 GIF。随意将其复制并粘贴到您的代码中:

    function resize_imagejpg($file, $w, $h, $finaldst) {
    
       list($width, $height) = getimagesize($file);
       $src = imagecreatefromjpeg($file);
       $ir = $width/$height;
       $fir = $w/$h;
       if($ir >= $fir){
           $newheight = $h; 
           $newwidth = $w * ($width / $height);
       }
       else {
           $newheight = $w / ($width/$height);
           $newwidth = $w;
       }   
       $xcor = 0 - ($newwidth - $w) / 2;
       $ycor = 0 - ($newheight - $h) / 2;
    
    
       $dst = imagecreatetruecolor($w, $h);
       imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight, 
       $width, $height);
       imagejpeg($dst, $finaldst);
       imagedestroy($dst);
       return $file;
    
    
    }
    
    
    
    
    
    
    function resize_imagegif($file, $w, $h, $finaldst) {
    
       list($width, $height) = getimagesize($file);
       $src = imagecreatefromgif($file);
       $ir = $width/$height;
       $fir = $w/$h;
       if($ir >= $fir){
           $newheight = $h; 
           $newwidth = $w * ($width / $height);
       }
       else {
           $newheight = $w / ($width/$height);
           $newwidth = $w;
       }   
       $xcor = 0 - ($newwidth - $w) / 2;
       $ycor = 0 - ($newheight - $h) / 2;
    
    
       $dst = imagecreatetruecolor($w, $h);
       $background = imagecolorallocatealpha($dst, 0, 0, 0, 127);
       imagecolortransparent($dst, $background);
       imagealphablending($dst, false);
       imagesavealpha($dst, true);
       imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight, 
       $width, $height);
       imagegif($dst, $finaldst);
       imagedestroy($dst);
       return $file;
    
    
    }
    
    
    
    function resize_imagepng($file, $w, $h, $finaldst) {
    
       list($width, $height) = getimagesize($file);
       $src = imagecreatefrompng($file);
       $ir = $width/$height;
       $fir = $w/$h;
       if($ir >= $fir){
           $newheight = $h; 
           $newwidth = $w * ($width / $height);
       }
       else {
            $newheight = $w / ($width/$height);
       $newwidth = $w;
       }   
       $xcor = 0 - ($newwidth - $w) / 2;
       $ycor = 0 - ($newheight - $h) / 2;
    
    
       $dst = imagecreatetruecolor($w, $h);
       $background = imagecolorallocate($dst, 0, 0, 0);
       imagecolortransparent($dst, $background);
       imagealphablending($dst, false);
       imagesavealpha($dst, true);
    
       imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, 
       $newheight,$width, $height);
    
       imagepng($dst, $finaldst);
       imagedestroy($dst);
       return $file;
    
    
    }
    
    
    
    
    
    
    
    
    function ImageResize($file, $w, $h, $finaldst) {
          $getsize = getimagesize($file);
          $image_type = $getsize[2];
    
          if( $image_type == IMAGETYPE_JPEG) {
    
             resize_imagejpg($file, $w, $h, $finaldst);
          } elseif( $image_type == IMAGETYPE_GIF ) {
    
             resize_imagegif($file, $w, $h, $finaldst);
          } elseif( $image_type == IMAGETYPE_PNG ) {
    
             resize_imagepng($file, $w, $h, $finaldst);
          }
    
    
    
    
    
    }
    

    你所要做的就是这样称呼它:

    ImageResize(image, width, height, destination);
    

    例如

    ImageResize("uploads/face.png", 100, 150, "images/user332profilepic.png");
    

    【讨论】:

    • 这对我有用!请问,为什么那些 resize 函数都返回空?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 2015-02-22
    • 2011-06-25
    • 2011-11-11
    • 2010-11-03
    • 1970-01-01
    相关资源
    最近更新 更多