Jump to content

imagepng($img_resource) - error!


Joshua4550

Recommended Posts

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 =)

Link to comment
https://forums.phpfreaks.com/topic/207692-imagepngimg_resource-error/
Share on other sites

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.