Hi All,
I need a bit of your help. I’m making a custom catalogue with a custom form to upload items to the database and later on displaying them using several templates.
I have managed to save all the data belonging to an item to the mysql database (inclusing up to 6 images per item) but now my problem comes when displaying the item. As the images are saved as mediumblob in mysql I have to make a custom php file to read the database, set header to the right content-type and flush the data.
How should I do it? In the template I have a snippet that prints something like <img src="getImage.php?id=xxx">.
I have the getImage.php file located somewhere in the webserver, in the php file I have $result = $modx->db->select(....) but as you can guess $modx is not defined beacuse it is an external php file and not a snippet or whatever.
Is this one the right direction to handle it? (I don’t want to save the images directly to the webserver file system).
How would you do it?.
Hope anyone have time to give me a hint!
Thanks!
Finally I got it working but without using dbapi. Just going directly agains mysql db with user/pass hardcoded :-(.
Any help to adapt it to use modx features and dbAPI?
img.php - Example code: <img src=’img.php?&id=3&table=xx&num=1&thumb’>
<?php
// Parameters:
// &id=id of the file
// &table=table where to lookup
// &i=number of image to retrieve
// &thumb get thumb data previously stored when uploading the image
$id = $_GET[’id’];
$enlace = mysql_connect(’server’, ’user’, ’pass’) or die(’Connect error : ’ . mysql_error());
mysql_select_db(’dbName’) or die(’Can’t connect to DB.’);
if(isset($id)) {
$num = $_GET [’num’];
$table = $_GET [’table’];
$query = "SELECT Img" .$num . " as data, ImgThumb" . $num . " as thumb, ImgType" . $num . " as filetype, ImgSize" . $num .
" as filesize FROM " . $table . " WHERE ID=’" . $id . "’";
$result = mysql_query($query) or die(’Error with query: ’ . mysql_error());
if ( $row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
if ( isset ( $_GET[’thumb’] ) ) $image= imagecreatefromstring($row [ ’thumb’ ]);
else $image= imagecreatefromstring($row [ ’data’ ]);
switch ($row["filetype"]){
case "image/jpeg":
header("Content-type: image/jpeg");
$display = imagejpeg ($image);
imagedestroy($image);
break;
case "image/png":
header("Content-type: image/png");
$display = imagepng($image);
imagedestroy($image);
break;
case "image/gif":
header ("Content-type: image/gif");
$display = imagegif($image);
imagedestroy($image);
break;
}
}
}
?>
-
☆ A M B ☆
- 24,524 Posts
Take a look at the index.ajax.php file in your MODx root. It is a good example of how to include the files needed to make the $modx object available.