简体   繁体   中英

Image cropping and thumb creation

I need to help a friend to make his own artistic gallery. I already have all done, but I need a tool/plugin/script to make him independent from me in uploading his own images. My gallery needs two images: a cropped one of a certain proportion (so i need him to crop by himself in an uploading page) and a thumb one (I want this be done automatically).

Do you know an easy way to do this? What would you do?

Thanks.

如果您不想开始拥有大量流量 - 请查看http://phpthumb.sourceforge.net/ ,它可以动态创建调整大小的图像。

Personally i use this in all of my projects - http://www.verot.net/php_class_upload.htm Works perfectly with upload files and with files that's already on the system.

Can do many things convert, resize and work on uploaded images in many ways, apply effects, add labels, watermarks and reflections and other image editing features.

Easy to work with.

You need only GD library, function imagecopyresampled will suit you. PHP manual has really good example code for thumbnail creation http://php.net/manual/en/function.imagecopyresampled.php You will need just to create exceptions for different file formats

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM