LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2005, 07:44 AM   #1
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Rep: Reputation: 30
Image sizes in PHP


Hello there. I need a method of finding out how large (memory-wise) an image is - it doesn't mastter when this is found out (ie. before or after the image is sent), but the size does need to be recorded in a MySQL database.

Anybody know how to do this?

Cheers.
 
Old 07-13-2005, 07:52 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I assume the image is uploaded as you said :
"(ie. before or after the image is sent)"

Say you named the upload field 'image' in your form,
then you could get the size as :
PHP Code:
$size filesize($_FILES['image']['tmp_name']); 
 
Old 07-13-2005, 07:54 AM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You could use the filesize() function to do that. Read this.

Edit: damn it, too slow.
 
Old 07-13-2005, 11:17 AM   #4
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Original Poster
Rep: Reputation: 30
No, it isn't uploaded - that'd be the easy way

It's an image proxy, to measure how much bandwidth is being used, and only send a small placeholder image after 200mb.

A code fragment is:
PHP Code:
case "jpg":
        
$orig imagecreatefromjpeg$imgurl );
        
header('Content-type: image/jpeg');
        
imagejpeg($orig);
        break; 
It loads the image resource directly from the site. It has to do this due to oddities in my hosting, where I only get a tiny amount of 'smart' CGI-enabled space and a huge amount of 'dumb' space for keeping images and the like.

I could have it download the image to a temporary file, but this way is much more efficient - if I can measure the image's size. It is in memory, after all - surely there's a way of finding out how much it's using?

The result has to be stored in a MySQL database. (I guess it could be stored in a file instead, but I don't really see the point...)
 
Old 07-13-2005, 09:07 PM   #5
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
What about the getimagesize() function?
 
Old 07-14-2005, 09:55 AM   #6
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Original Poster
Rep: Reputation: 30
No, I looked at that - as far as I can tell, it takes the path to a saved file as input and gives you it's physical sizes (ie. width x height) rather than its data size.

I've decided to do it the inefficient way now - it saves the file to disk using the copy() function, then gets its size before loading it in as an image.

Thanks for all your help though, it's greatly appreciated.

Hmm, would this constitute a possible feature request?
 
Old 07-14-2005, 12:44 PM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You could get the size from the gd resource with something like :
PHP Code:
...
$orig imagecreatefromjpeg$imgurl );
header('Content-type: image/jpeg');
ob_start();
imagejpeg($orig);
$image_string ob_get_contents();
$image_size strlen($image_string); 
then in $image_size, you should have the image size in bytes
 
Old 07-15-2005, 10:01 AM   #8
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Original Poster
Rep: Reputation: 30
Thanks, but I've realised that the way I've used is actually better now.

It measures the exact filesize transferred from the storage space, which is what I need to know - if the script re-encodes the image itself, its ouput might be a different length.

Thanks for the method, though - I'll refer back to this next time I'm confronted with a similar problem!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GD image streams and PHP TylerD75 Programming 2 11-14-2005 04:04 PM
php and gd (image functions) devfreak Linux - Software 1 05-11-2005 05:17 PM
Php image optimize script htmlcoder Programming 0 03-19-2005 03:23 AM
PHP 4.3 Upgrade for Image Libraries SForsgren Linux - Software 1 05-21-2003 07:00 AM
The best image manipuliation way in to PHP dr. Agon Programming 0 01-28-2001 07:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration