Solved

Get image data pointed to by a Resource id

Posted on 2002-03-08
4
329 Views
Last Modified: 2012-05-04
I am currently working on a gallery project.

I have written code to store clients photos in a Mysql database, which is all working okay.  I want to create a thumbnail on the fly which I do with the following code :-

$def_thumb_width=100;
$def_thumb_height=75;
$thumb_image_name= "t_" . $original_image_name;

$scaling_width=$original_image_width / $def_thumb_width;
$scaling_height=$original_image_height / $def_thumb_height;
if ($scaling_width > $scaling_height)
{
     $thumb_width=$original_image_width / $scaling_width;
     $thumb_height=$original_image_height / $scaling_width;
}
else
{
     $thumb_width=$original_image_width / $scaling_height;
     $thumb_height=$original_image_height / $scaling_height;
}
$thumb_width= ceil($thumb_width);
$thumb_height= ceil($thumb_height);

$im=imagecreatefromjpeg("$original_image");
$thumb=imagecreate($thumb_width,$thumb_height);
imagecopyresized($thumb,$im,0,0,0,0,$thumb_width,$thumb_height,$original_image_width,$original_image_height)

I have used fopen to open the original file in order to get the binary for the file. To prevent writing the data out to a file and reading it back again, I need to gain access to the data. $thumb will contain the image resource, and ImageJPEG($thumb) will output the data to the screen but I want it sent to my database instead.

In short how do I access data pointed to by a resource id.
0
Comment
Question by:waygood
  • 2
4 Comments
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
If you are storing the images in the database, then you may as well do it off line.

I use a program called ThumbsPlus on Windows. It can write to MySQL databases no problems.


But to answer your question, you can add a file name to

ImageJPEG($thumb,"/home/images/saved.jpg");

and then put that file in the database the same way you did the main image.

I cannot see a standard function to allow you to access the data directly.

BUT, what would happen if you sent the data to STDOUT (or something similar) and caught it via another program/script? No idea if possible.

Regards,

Richard Quadling.
0
 
LVL 9

Author Comment

by:waygood
Comment Utility
I did capure the data as a work around using :-

     ob_start();
     Imagejpeg($thumb);
     
     $thumb_image_size=ob_get_length();
     $thumb_data=addslashes(ob_get_contents());
     ob_end_clean();

but fail to see why I need to output data and recapture it again through buffering of saving as a file. The data is there, I just need to access it.

P.S. I am writing this for people who are very stupid. They don't know how to deal with more than one program open at a time, let alone how to create thumbnails. I gave them Mihov's Image Resizer (freeware) and they complained when it doubled the number of images they had (extra thumbnails). Duh!
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 75 total points
Comment Utility
I agree with you that is seems that PHP does not give you full access to its objects. I am surprised that there is not a standard method for accessing the raw data, even in a readonly form.

As I mentioned earlier, ThumbsPlus will store the thumbnails into a database (even an MySQL database!).

It has scripting so that you can basically convert a whole load of images into thumbnails and store them in the database.

I can't think of anyother method for you. Offline conversion and storing in a DB would reduce the number of images to 1 extra file.

Ho hum!

Regards,

Richard Quadling.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now