Jump to content

Help with GD library


JeremyCanada26

Recommended Posts

I'm using the GD library to manipulate a png image and I'd like to know how to get access to a byte array of the data so that I can use a third party function that takes in a png bytearray and sends it off to a webservice for storage.

 


//fetch an image from the web
$image = imagecreatefrompng($url);

//I do other GD manipulations with the $image resource above so I need it to come in as an image resource
doOtherStrangeThings();

// where $width and $height are the dimensions of the final image
$final_img = imagecreate($width, $height);

//get access to the bytearray ????
$finalImageByteArray = imagepng($image, "0", PNG_NO_FILTER)

//save the new image to third party storage service, must be a png bytearray
$this->_saveImage($finalImageByteArray);

 

The above code generates an error message for me. imagepng(): Unable to open '0' for writing: Permission denied but i'm not trying to write the file, I want to get access to the image resource as a bytearray. It's already a png image so I suspect it has the png header already in tact, etc

Link to comment
Share on other sites

the second paramater is for saving files

you need this:

 

function OutputImage($img) 
{
ob_start();
imagejpeg($img, null, 100);
$page = ob_get_contents();
ob_end_clean(); // use ob_end_clean if you don't want to display the image  
return $page;
}

 

I will definitely try this when I get home. I actually require a png image so i'll probably try using the imagepng($img, null, PNG_NO_FILTER);

 

if that works, your a genius :)

 

Link to comment
Share on other sites

The documentation for the s3 php class says it can take an input resource but it looks like it might be only for file resources and not image resources.

 

http://undesigned.org.za/2007/10/22/amazon-s3-php-class/documentation#inputResource

 

it says

 

 

inputResource (resource &$resource, integer $bufferSize, [string $md5sum = '']) Back to top

 

Create an input array for pubObject() with a resource

 

Example:

 

<?php

 

    $file = "file.txt";

    $input = $s3->inputResource(fopen($file, "rb"), filesize($file));

    if (S3::putObject($input, $bucket, $uri, S3::ACL_PUBLIC_READ)) {

        echo "File uploaded.";

    } else {

        echo "Failed to upload file.";

    }

 

?>

 

 

Does anyone know how to convert that code to use an image resource instead of a file resource?

Link to comment
Share on other sites

the second paramater is for saving files

you need this:

 

function OutputImage($img) 
{
ob_start();
imagejpeg($img, null, 100);
$page = ob_get_contents();
ob_end_clean(); // use ob_end_clean if you don't want to display the image  
return $page;
}

 

Ok, I finally got home to try this and now my image is not readable so something went wrong. I tried both with your imagejpeg($img, null, 100) as well as with imagepng($img, NULL, 4); and both produce unreadable png images that do not work.

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.