图像裁剪和拇指创作

问题描述:

我需要帮助朋友建立自己的艺术画廊。 我已经全部完成了,但是我需要一个工具/插件/脚本来让他独立于我,无需上传他自己的图片。我的画廊需要两个图像:一个裁剪的比例(所以我需要他自己在上传页面裁剪)和一个拇指(我想这是自动完成)。图像裁剪和拇指创作

你知道一个简单的方法来做到这一点吗?你会怎么做?

谢谢。

+1

使用这个答案图像大小调整和创建缩略图http://stackoverflow.com/questions/10069981/图像获取拉伸/ 10070222#10070222 – nithi

我个人使用这个在我的所有项目中 - http://www.verot.net/php_class_upload.htm 与上传文件和系统中已有的文件完美配合。

可以通过多种方式对上传的图像进行转换,调整大小和工作,应用效果,添加标签,水印和反射以及其他图像编辑功能。

易于使用。

如果你不会有繁忙的流量开始 - 看http://phpthumb.sourceforge.net/它可以创建您的动态调整大小的图像。

您只需要GD库,函数imagecopyresampled将适合您。 PHP手册中有创建缩略图真的很好的例子代码http://php.net/manual/en/function.imagecopyresampled.php 你只需要为不同的文件格式创建例外

function create_jpeg_thumbnail($thumbImageName,$imgSrc,$thumbDirectory,$thumbnail_width,$thumbnail_height) { //$imgSrc is a FILE - Returns an image resource. 
     $thumbDirectory = trim($thumbDirectory); 
     $imageSourceExploded = explode('/', $imgSrc); 
     $imageName = $imageSourceExploded[count($imageSourceExploded)-1]; 
     $imageDirectory = str_replace($imageName, '', $imgSrc); 
     $filetype = explode('.',$imageName); 
     $filetype = strtolower($filetype[count($filetype)-1]); 

     //getting the image dimensions 
     list($width_orig, $height_orig) = getimagesize($imgSrc); 


     //$myImage = imagecreatefromjpeg($imgSrc); 
     if ($filetype == 'jpg') { 
      $myImage = imagecreatefromjpeg("$imageDirectory/$imageName"); 
     } else 
     if ($filetype == 'jpeg') { 
      $myImage = imagecreatefromjpeg("$imageDirectory/$imageName"); 
     } else 
     if ($filetype == 'png') { 
      $myImage = imagecreatefrompng("$imageDirectory/$imageName"); 
     } else 
     if ($filetype == 'gif') { 
      $myImage = imagecreatefromgif("$imageDirectory/$imageName"); 
     } 

     $ratio_orig = $width_orig/$height_orig; 

     if ($thumbnail_width/$thumbnail_height > $ratio_orig) { 
      $new_height = $thumbnail_width/$ratio_orig; 
      $new_width = $thumbnail_width; 
     } else { 
      $new_width = $thumbnail_height*$ratio_orig; 
      $new_height = $thumbnail_height; 
     } 

     $x_mid = $new_width/2; //horizontal middle 
     $y_mid = $new_height/2; //vertical middle 

     $process = imagecreatetruecolor(round($new_width), round($new_height)); 

     imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); 
     $thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height); 
     imagecopyresampled($thumb, $process, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height); 
     //$thumbImageName = 'thumb_'.get_random_no().'.jpeg'; 
     $destination = $thumbDirectory=='' ? $thumbImageName : $thumbDirectory."/".$thumbImageName; 
     imagejpeg($thumb, $destination, 100); 
     return $thumbImageName; 
    }