Question

image_small.php looks like this:

<?php
$id = (isset($_GET['im'])) ? $_GET['im'] : 0;

//fetch the image from database
$query='SELECT image_small FROM hr_employee WHERE id='.$id;
$result = pg_query($dbconn, $query) or die('Query failed: ' . pg_last_error());

$raw = pg_fetch_row ( $result ,0)
$img=$raw['image_small'];

header("Content-Type: image/png");

if( !file_exists($cachefile) ) {
    imagepng($img, $cachefile);
    imagedestroy($img);
}

$fp = fopen($cachefile, 'rb');
fpassthru($fp);
exit;
?>

I get the following error:

imagepng() expects parameter 1 to be resource, string given

Was it helpful?

Solution 2

The error is very explicit, imagepng expects a resource. To be simple, imagine a resource as an object from which you cant't know the class. In fact, GD is developped in C language, and PHP wraps all unknown C structures inside resources.

So, you need to ask GD to create this resource for you, before using gd-family functions. If you stored your image raw, you'll get it raw and will need to use imagecreatefromstring function.

$gdh = imagecreatefromstring($img);
imagepng($gdh, $cachefile);

OTHER TIPS

It looks to me as if your database contains base64 encoded image data.

Try Changing your code to

$img = base64_decode($raw['image_small']);

and see whether the output changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top