Eric Smith
Eric Smith

Reputation: 1376

Image cropping in php

So I am trying to write a little script to crop a users image. I would send some information (width, height, alignment properties and image url) to the script and it should return the image cropped. However, it's not working... Just an "image not found" symbol :/ Here is my script, any thoughts?

<?php
session_start();
header('Content-type: image/jpeg');
$w=$_GET['w'];
$h=$_GET['h'];
$x=$_GET['x'];
$y=$_GET['y'];
$filename="http://www.domain.com/".$_GET['src'];
$file_ext = substr($filename, strrpos($filename, ".") + 1);
$ext='';

if($file_ext=='jpg')
{
    $image = imagecreatefromjpeg($filename); 
}
else if ($file_ext=='gif')
{
    $image = imagecreatefromgif($filename); 
}
else if ($file_ext=='png')
{
    $image = imagecreatefrompng($filename); 
} 

$crop = imagecreatetruecolor($w,$h);
imagecopy($crop, $image, 0, 0, $x, $y, $w, $h);
imagejpeg($crop);
?>

Edit: Looks like this is the error: Fatal error: Call to undefined function imagecreatetruecolor() in domainpath/crop.php on line 24 Is there anything I need to do to load this function?

Upvotes: 0

Views: 3120

Answers (3)

Blakdronzer
Blakdronzer

Reputation: 340

I will recommend u use imagemagick instead of GD / GD2 lib. If u looking for quality, imagemagick is too good for the same. There is this link which also gives compares on the same http://www.rubblewebs.co.uk/imagemagick/compair.php

Upvotes: 0

JYelton
JYelton

Reputation: 36546

According to your comment about the error, imagecreatetruecolor() is indeed a function, but only if you have the GD library loaded.

Make sure you have a compatible version of GD accessible to your PHP installation, and don't forget to restart your web server if you have just added it.

Upvotes: 2

Alfred
Alfred

Reputation: 21396

Using ImageMagick, try this;

<?php
function resize_image($file, $w, $h, $crop=FALSE) {
    $img = new Imagick($file);
    if ($crop) {
        $img->cropThumbnailImage($w, $h);
    } else {
        $img->thumbnailImage($w, $h, TRUE);
    }

    return $img;
}
resize_image(‘/path/to/some/image.jpg’, 150, 150);

Upvotes: 1

Related Questions