Francesco Bonizzi
Francesco Bonizzi

Reputation: 5302

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.

Upvotes: 1

Views: 165

Answers (4)

Ravi Sharma
Ravi Sharma

Reputation: 1182

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

Upvotes: 1

Octopussy
Octopussy

Reputation: 142

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

Upvotes: 1

arma
arma

Reputation: 4124

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.

Upvotes: 1

a1phanumeric
a1phanumeric

Reputation: 854

If you're not going to be having heavy traffic to start - look at http://phpthumb.sourceforge.net/ which can create your resized images on the fly.

Upvotes: 1

Related Questions