2

I came up with this:

<?php 

$dir = $_GET['dir'];

header('Content-type: image/jpeg'); 

$create = imagecreatetruecolor(150, 150); 
$img = imagecreatefromjpeg($dir); 
imagecopyresampled($create, $img, 0, 0, 0, 0, 150, 150, 150, 150); 

imagejpeg($create, null, 100); 

?>

It works by accessing:

http://example.com/image.php?dir=thisistheimage.jpg

Which works fine... but the output is awful:

alt text

Can someone fix my code for the image to be 150 x 150 covering the black area...

Thanks.

SOLUTION:

<?php 

$dir = $_GET['dir'];

header('Content-type: image/jpeg'); 

list($width, $height) = getimagesize($dir);

$create = imagecreatetruecolor(150, 150); 
$img = imagecreatefromjpeg($dir); 

$newwidth = 150;
$newheight = 150;

imagecopyresized($create, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($create, null, 100); 

?>
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
MacMac
  • 34,294
  • 55
  • 151
  • 222
  • You could also use .htaccess to rewrite URL and have paths like: http://domain.com/imagephp/thisistheimage.jpg – Milan Babuškov May 30 '10 at 20:20
  • You should save a copy of the resized image with the width/height parameters in the filename and check if it exists before processing and serve that one so that you keep the load on the server down. Also use imagedestroy($create) after imagejpeg() to free up memory. – stagas May 30 '10 at 20:26

3 Answers3

6

Use imagecopyresized:

$newwidth = 150;
$newheight = 150;
imagecopyresized($create, $image, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
1

The last 2 150 should be the original width and height of the full sized image.

CharlesLeaf
  • 3,201
  • 19
  • 16
  • Better left as a comment. This isn't an answer. – stagas May 30 '10 at 20:28
  • 3
    It was a perfectly correct answer on the ORIGINAL unmodified question. – CharlesLeaf May 30 '10 at 21:13
  • Sorry, I'm trying to undo the down vote but it says it's locked. Still, it's a lazy answer, you should probably delete it. – stagas May 31 '10 at 03:02
  • 2
    It's ok I don't care about the vote down. What makes it a lazy answer, that I didn't write/correct the code for him myself? I don't think that would have helped him at all, yes it would have *worked* but would he have learned what the problem was? I prefer to give gentle pushes in the right direction. – CharlesLeaf May 31 '10 at 07:04
1

As others suggested, last two parameters should be original size of the image.

If $dir is your filename, you can use getimagesize to obtain picture's original dimensions.

You can use imagecopyresized or imagecopyresampled. Difference is that imagecopyresized will copy and resize while imagecopyresampled will also resample your image which will yield better quality.

<?php 

$dir = $_GET['dir'];

header('Content-type: image/jpeg'); 

$create = imagecreatetruecolor(150, 150); 
$img = imagecreatefromjpeg($dir);
list($width, $height) = getimagesize($dir);
imagecopyresampled($create, $img, 0, 0, 0, 0, 150, 150, $width, $height);

imagejpeg($create, null, 100); 

?>
Luc
  • 1,488
  • 1
  • 13
  • 31