This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-11-14 16:06:06

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

PHP image help

So I have a PHP program where I need to output the image as a data URI for inclusion in an <img> tag. Here's the relevant code:

Code:

...
ob_start();
imagepng($img);
$data = 'data:image/png;base64,' . base64_encode(ob_get_contents());
ob_end_clean();
...

$img has already been generated.

The code runs and outputs the data URI, but it seems the URI is invalid - the image displays as the broken image icon. Can anybody help?


/* No comment */

Offline

 

#2 2012-11-14 16:39:13

jvvg
Scratcher
Registered: 2008-03-26
Posts: 1000+

Re: PHP image help

It should work...  hmm

I have experience with data URIs on Mod Share and we use the base64_encode function. Are you sure it's a valid image?


http://tiny.cc/zwgbewhttp://tiny.cc/e1gbewhttp://tiny.cc/zygbewhttp://tiny.cc/izgbew
Goodbye, Scratch 1.4  sad                                                        Hello Scratch 2.0!  smile

Offline

 

#3 2012-11-15 14:16:41

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Re: PHP image help

jvvg wrote:

Are you sure it's a valid image?

Just checked, and it seems imagepng isn't working properly. imagegif and imagejpeg work, but imagepng won't even output a valid image straight off. However, the image output by the other two functions is just a black rectangle. Is there something wrong with my code, or the image library?

Code:

$img = imagecreatetruecolor(100, 20);
$back = imagecolorallocate($image, 255, 255, 255);
imagefill($img, 0, 0, $back);
$col = imagecolorallocate($image, 255, 0, 0);
imagestring($img, 1, 0, 0, 'test', $col);
ob_start();
imagegif($img);
$data = 'data:image/gif;base64,' . base64_encode(ob_get_contents());
ob_end_clean();
imagedestroy($img);
return $data;

/* No comment */

Offline

 

#4 2012-11-15 15:18:35

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: PHP image help

TheSuccessor wrote:

jvvg wrote:

Are you sure it's a valid image?

Just checked, and it seems imagepng isn't working properly. imagegif and imagejpeg work, but imagepng won't even output a valid image straight off. However, the image output by the other two functions is just a black rectangle. Is there something wrong with my code, or the image library?

Code:

$img = imagecreatetruecolor(100, 20);
$back = imagecolorallocate($image, 255, 255, 255);
imagefill($img, 0, 0, $back);
$col = imagecolorallocate($image, 255, 0, 0);
imagestring($img, 1, 0, 0, 'test', $col);
ob_start();
imagegif($img);
$data = 'data:image/gif;base64,' . base64_encode(ob_get_contents());
ob_end_clean();
imagedestroy($img);
return $data;

Maybe it's just a problem with the server.


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#5 2012-11-15 18:42:57

GeonoTRON2000
Scratcher
Registered: 2009-12-24
Posts: 1000+

Re: PHP image help

TheSuccessor wrote:

jvvg wrote:

Are you sure it's a valid image?

Just checked, and it seems imagepng isn't working properly. imagegif and imagejpeg work, but imagepng won't even output a valid image straight off. However, the image output by the other two functions is just a black rectangle. Is there something wrong with my code, or the image library?

Code:

$img = imagecreatetruecolor(100, 20);
$back = imagecolorallocate($image, 255, 255, 255);
imagefill($img, 0, 0, $back);
$col = imagecolorallocate($image, 255, 0, 0);
imagestring($img, 1, 0, 0, 'test', $col);
ob_start();
imagegif($img);
$data = 'data:image/gif;base64,' . base64_encode(ob_get_contents());
ob_end_clean();
imagedestroy($img);
return $data;

yikes  Just found it.  Take a closer look at line 2.  You used $image instead of $img.


http://i.imgur.com/BAEgGDL.png

Offline

 

#6 2012-11-16 12:50:00

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Re: PHP image help

GeonoTRON2000 wrote:

yikes  Just found it.  Take a closer look at line 2.  You used $image instead of $img.

Derp  tongue

It works now. Thanks to everyone.


/* No comment */

Offline

 

Board footer