Jump to content

[SOLVED] Image from MySQL + GD Problem!


Hyaku_

Recommended Posts

Hi!
I have image saved in MySQL. For website I need to resize it, but I don't know how to open image with GD from MySQL. This is how far I got:
img.php
[code]<?
include($_SERVER['DOCUMENT_ROOT'] . "/my_sql.php");

$query = "SELECT IMG, IMG_TYPE FROM USER_ACCOUNT WHERE ID = 1";
$query_exec = mysql_query($query);
list($img, $type) = mysql_fetch_array($query_exec);

Header("Content-Type: $type");
echo $img;

?>[/code]

img_gd.php
[code]<?
header("Content-type: image/jpeg");
$img = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'] . "/img.php");

imagejpeg($img, "", 100);
imagedestroy($img);

?>[/code]

But I get:
[code]imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error
Warning:  imagecreatefromjpeg(): '/var/www/htdocs/include/img.php' is not a valid JPEG file[/code]

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/33434-solved-image-from-mysql-gd-problem/
Share on other sites

yeah, but img.php has:
[code]Header("Content-Type: $type");[/code]
in it. I thought it would do the trick..

ok, the only way now that comes into my mind is to read the image from mysql and save it into tmp directory and then open it thru gd.. if anyone knows any other way, let me know! Thanks!
actually the problem may lie in the fact that u put in $_SERVER['DOCUMENT_ROOT']

the file itself when being referenced most likely is not being referenced as /home/yoursite/public_html or however your site sees $_SERVER['DOCUMENT_ROOT'] 

if i call $_SERVER['DOCUMENT_ROOT']  . "index.php" on my site it will return http://www.collegebookx.com/home/collegeb/public_html/index.php  when i reality that will not work

try it without the $_SERVER['DOCUMENT_ROOT']  and maybe try just img.php or ../img.php
Thanks!
Then the error would be probably about "file not found" or something.. anyway I tried, but unfortunetly that didn't work, so I thought if imagecreatefromjpeg checks for file extension, I could pass query:
[code]img.php?some.jpg[/code]
but it looks like it doesn't understand how to handle queries.. If it checks file type just by extension, not the actual header, I could rename my img.php to img.jpg and add another file type for php to execute.
[code]AddType application/x-httpd-php .jpg
[/code]
I wonder if that would work.. :-X
ok, now all the files that end with extention *.jpeg are executed as PHP source files, so I renamed my img.php script to img.jpeg, executed it, and it worked, but when I try to:
[code]$img = imagecreatefromjpeg("img.jpeg");[/code]
still the same error.. 
guess it checks the file type by some other way :-\

Thanks! I have my own server and I use SSH to access it.
Okay, I am fairly new to this too.
But isn't the function you are looking for:
[url=http://se.php.net/imagecreatefromstring]http://se.php.net/imagecreatefromstring[/url]

[code]$query = "SELECT IMG, IMG_TYPE FROM USER_ACCOUNT WHERE ID = 1";
$query_exec = mysql_query($query);
list($img, $type) = mysql_fetch_array($query_exec);

$image = imagecreatefromstring($img);[/code]

Then $image will be the resource which you can do a thumbnail with.
Hope it works :)

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.