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
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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.