Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 3rd, 2004, 05:54 PM
Registered User
 
Join Date: Mar 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Resizing Images From a Database

I have image files in a database, and the institution I'm working for won't give users permission to write files to the directory structure. So what I want to do is pull an image from the database, then resize it to thumbnail size for display. The PHP is running version 4.2.2, and GD version 1.6, so i have to use imagecopyresize, not imagecopyresample. Also, it uses a PostGRES database.

I can successfully pull the image from the database and display it, using pg_lo_open(), which makes a large object identifier, and pg_lo_read_all(), which outputs the large object data to the browser. All I have to do is make the header of type image/jpeg.

However, the object ID I get from the database doesn't seem to be compatible with the imagecopyresize function. It says:

"Warning: imagecopyresized(): supplied resource is not a valid Image resource"

I tried writing it to the buffer, using imagejpeg(), but it still gives me the same error.

Anyone have any ideas? I guess the question boils down to how to convert a large object resource to an image resource. I would have thought that data resources would be interchangable.

Here is my code:



Code:
<?php 
    // open database.
    $conn = pg_connect("port=5432 dbname=mseavey");
    if (!$conn) {
       echo "An error occured connecting to the database.<BR>";
        exit;
    }

    // query the image.
    $imageQuery = "SELECT filedata, filetype FROM filetest WHERE id = ".$_GET['id'];
   $rs = pg_exec($conn, $imageQuery);

   $row = pg_fetch_row($rs, 0);

   pg_exec($conn, "begin");
   $loid = pg_lo_open($conn, $row[0], "r");

   // try to get $loid to be read by the resize function.
   ob_start();
   imagejpeg($loid);
   $buffer = ob_get_contents();
   ob_end_clean();

    $sloid = imagecreate(100, 100);

   // resize the image.
   if (imagecopyresized($sloid, $buffer, 0, 0, 0, 0, 100, 100, 477, 478)) {
        imagejpeg($sloid);
    } else {
        echo "Error resizing image.";
    }
   pg_loclose($loid);
   pg_exec ($conn, "commit");

    pg_close();
?>
 
Old March 3rd, 2004, 07:19 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

The problem with this is.. and I know because I've designed an identical application... is that you cannot supply the binary data itself to the image resize functions. You have to call up the image using an external script, whereas you will be able to create a valid resource identifier. In your script $buffer is the binary data itself, whereas imagecopyresized expects only a resource identifier.

The following is a snip from how my own program handles this. If you want to look at the class I wrote to do this I can send it along, but it'll be up to you to make sense of it.

Code:
$source_file = imagecreatefromjpeg('http://path/to/external/viewer/script.php');

// I'm using a URL here because this function expects 
// a file path or URL, and I want the PHP script to be executed
// so it will output the image from the DB, so I use a URL.

$destination_file = imagecreate($resize_width, $resize_height);
imagecopyresized($destination_file, $source_file, 0, 0, 0, 0, $resize_width, $resize_height, $actual_width, $actual_height);

ob_start();
imagejpeg($destination_file, null, -1);
$data = addslashes(ob_get_contents());
ob_end_clean();

// Now write a query to insert the thumbnail $data into the DB

imagedestroy($source_file);
imagedestroy($destination_file);
hth,
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::





Similar Threads
Thread Thread Starter Forum Replies Last Post
Load Images from and Save Images to a Database cyndie VB.NET 2 August 17th, 2008 06:42 AM
uploading/resizing (transparant) images in C# smiter C# 1 February 19th, 2007 08:53 PM
Resizing Images san123 ASP.NET 1.0 and 1.1 Basics 2 April 26th, 2006 02:51 PM
Resizing images according to paper size Taxidriver Crystal Reports 3 March 28th, 2006 06:08 AM
GDI+ - Issue Resizing images Hubman VS.NET 2002/2003 2 April 16th, 2004 08:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.