Joshua4550 Posted July 14, 2010 Share Posted July 14, 2010 Hey, I was recently refferred to a few of the GD functions to complete a task I asked if was possible or not - and i'm SO close to finishing. I have the method polished up, I just need to find out why my image is not outputting. I made the script crop the image and copy it onto the blank canvas, but when I try to output it, it simply outputs: ‰PNG ��� IHDR���ƒ���i���– Õ���?IDATxœíÁ1��� õOm ? ����������������������������������������^¡š�$û����IEND®B`‚ Does anyone know the general/most common causes of this? Just need to fix and I'm done =) Quote Link to comment https://forums.phpfreaks.com/topic/207692-imagepngimg_resource-error/ Share on other sites More sharing options...
Alex Posted July 14, 2010 Share Posted July 14, 2010 That's the output of the image. If you want to view it as an image then you need to send the appropriate headers. header('Content-type: image/png'); Quote Link to comment https://forums.phpfreaks.com/topic/207692-imagepngimg_resource-error/#findComment-1085729 Share on other sites More sharing options...
Joshua4550 Posted July 14, 2010 Author Share Posted July 14, 2010 Okay, I tried this before - but it gave me the following error: The image “http://mysite.com/test/” cannot be displayed, because it contains errors. Atm, here's my codes: (I've tried multiple ways around this, but not found a working one) function fitToCanvas($img) { $size = getimagesize($img); $width = $size[0]; $height = $size[1]; $img_resource = imagecreatefrompng($img); $widths = array(); $heights = array(); $count = 0; for ($i = 0; $i < $height; $i++) { for ($j = 0; $j < $width; $j++) { $colorAt = imagecolorat($img_resource, $j, $i); $colorIndex = imagecolorsforindex($img_resource, $colorAt); if ($colorIndex['alpha']!=127) { // Not transparrent, load this pixel into the array $count++; $widths[$count] = $j; $heights[$count] = $i; } } } $bottom_right = array(max($widths), max($heights)); $top_left = array(min($widths), min($heights)); $image_size = array($bottom_right[0]-$top_left[0], $bottom_right[1]-$top_left[1]); $img2 = imagecreatetruecolor($image_size[0], $image_size[1]); imagecopy($img2, $img_resource, 0, 0, 0, 0, $image_size[0], $image_size[1]); imagedestroy($img_resource); return $img2; } header('Content-type: image/png'); echo '<img src="lolhi.png" title="" />'; $img = fitToCanvas("lolhi.png"); imagepng($img); echo '<br /><br />'; echo '<img src="fgt.png" title="" />'; $img = fitToCanvas("fgt.png"); imagepng($img); echo '<br /><br />'; I know the method isn't most efficient, was going to go and seek help with improving it if it runs slow, later on. Any idea how to move this around to make the header not give me errors, and not give the "headers already sent" error? Quote Link to comment https://forums.phpfreaks.com/topic/207692-imagepngimg_resource-error/#findComment-1085730 Share on other sites More sharing options...
Joshua4550 Posted July 14, 2010 Author Share Posted July 14, 2010 Any1? Quote Link to comment https://forums.phpfreaks.com/topic/207692-imagepngimg_resource-error/#findComment-1085744 Share on other sites More sharing options...
Joshua4550 Posted July 14, 2010 Author Share Posted July 14, 2010 Ookay, I got the image to print - but now it's a black canvas. I'll work on fixing this atm, but does anyone see where my method code goes wrong? Quote Link to comment https://forums.phpfreaks.com/topic/207692-imagepngimg_resource-error/#findComment-1085839 Share on other sites More sharing options...
Joshua4550 Posted July 14, 2010 Author Share Posted July 14, 2010 Right, so I got the code to work - but the background isn't transparrent, anyone know why? function fitToCanvas($img) { $size = getimagesize($img); $width = $size[0]; $height = $size[1]; $img_resource = imagecreatefrompng($img); $widths = array(); $heights = array(); $count = 0; for ($i = 0; $i < $height; $i++) { for ($j = 0; $j < $width; $j++) { $colorAt = imagecolorat($img_resource, $j, $i); $colorIndex = imagecolorsforindex($img_resource, $colorAt); if ($colorIndex['alpha']!=127) { // Not transparrent, load this pixel into the array $count++; $widths[$count] = $j; $heights[$count] = $i; } } } $bottom_right = array(max($widths), max($heights)); $top_left = array(min($widths), min($heights)); $image_size = array($bottom_right[0]-$top_left[0], $bottom_right[1]-$top_left[1]); $img2 = imagecreatetruecolor($image_size[0], $image_size[1]); $im = imagecopy($img2, $img_resource, 0, 0, $top_left[0], $top_left[1], $image_size[0], $image_size[1]); imagedestroy($img_resource); return $img2; } Quote Link to comment https://forums.phpfreaks.com/topic/207692-imagepngimg_resource-error/#findComment-1085849 Share on other sites More sharing options...
Joshua4550 Posted July 14, 2010 Author Share Posted July 14, 2010 lolol nvm i did it all. Quote Link to comment https://forums.phpfreaks.com/topic/207692-imagepngimg_resource-error/#findComment-1085855 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.