图像裁剪和拇指创建


Image cropping and thumb creation

我需要帮助一个朋友制作自己的艺术画廊。我已经完成了所有工作,但我需要一个工具/插件/脚本来让他在上传自己的图像时独立于我。我的画廊需要两张图片:一张按一定比例裁剪的图片(所以我需要他在上传页面中自己裁剪)和一张拇指图片(我希望这自动完成)。

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

谢谢。

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

可以做很多事情,以多种方式转换,调整大小和处理上传的图像,应用效果,添加标签,水印和反射以及其他图像编辑功能。

易于使用。

如果您不会开始遇到繁忙的流量 - 请查看 http://phpthumb.sourceforge.net/可以即时创建调整大小的图像。

您只需要GD库,函数图像复制重新采样将适合您。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;
    }