Jump to content

PHP PNG resize?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
:(
The following code fetches a skin and crops it, however as the result is tiny I wish to re-size it. I have tried imagecopy, imagecopyresize but decided to use imagecopyresampled as it apparently makes the final look nicer :).

Sadly NONE of these functions work and instead give me a black 128X128 square! Any ideas?
<?php

	$image = imagecreatefrompng('http://s3.amazonaws.com/MinecraftSkins/bbqr0ast.png');

	$crop = imagecreatetruecolor(128,128);

	$userImage = imagecopyresampled($crop, $image, 0, 0, 0, 0, 128, 128, 8, 8);

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

	imagepng($crop);

?>

Edited by bbqroast, 06 July 2011 - 11:31 PM.

Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#2
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
NVM This. Check Alexander's post below

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Remember to carefully review the synopsis of each function, they often vary between similar functions due to historic order.

http://php.net/manua...yresampled.php:
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

You seem to specify the source image height as eight and eight, even though likely your image is much larger than that. It will therefor resample it much lower than the original size regardless of destination point (128x128)

If you retrieve the image size with getimagesize on the orignal image, i.e.
list($width, $height) = getimagesize($url);

You will get the appropriate result:

Posted Image


Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
The minecraft skins are tiny, http://s3.amazonaws....ns/bbqr0ast.png, that is 8 pixels of "head" area.
I only want to get the "head" area, ahhhh I think I am forgetting to feed the cords properly :(.
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#5
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
Ok, fixed:
<?php

	$image = imagecreatefrompng('http://s3.amazonaws.com/MinecraftSkins/bbqr0ast.png');

	$crop = imagecreatetruecolor(128 , 128);

	imagecopyresampled($crop, $image, 0, 0, 8, 8, 128, 128, 8, 8); 

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

	imagepng($crop);

?>
Errors:
I was not specifying the y and x values for the head part of the original image (8,8).
On my second line I set the image to 8X8 pixels, this meant that when I pasted my head as 128X128 pixels only a portion of the top left corner came out (which was black as that's my hair!).
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users