Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 05-01-2005, 07:55 PM pictures with PHP?
Recrehal's Avatar
Experienced Talker

Posts: 39
Location: Aachen, Germany
Trades: 0
PHP Code:
/**
* Resize an image and keep the proportions
* @author Allison Beckwith <allison@planetargon.com>
* @param string $filename
* @param integer $max_width
* @param integer $max_height
* @return image
*/
function resizeImage($filename$max_width$max_height)
{
   list(
$orig_width$orig_height) = getimagesize($filename);

   
$width $orig_width;
   
$height $orig_height;

   
# taller
   
if ($height $max_height) {
       
$width = ($max_height $height) * $width;
       
$height $max_height;
   }

   
# wider
   
if ($width $max_width) {
       
$height = ($max_width $width) * $height;
       
$width $max_width;
   }

   
$image_p imagecreatetruecolor($width$height);

   
$image imagecreatefromjpeg($filename);

   
imagecopyresampled($image_p$image0000
                                     
$width$height$orig_width$orig_height);

   return 
$image_p;

</FONT>

I want to use this function, but I never worked with graphics before. When I echo the output of this function I get ressourced ID 8 and if I use
PHP Code:
echo '<img src="'.imagepng(resizeImage(bla.jpg10,10)).'" alt="">'
</SPAN>

I get the coredump. So how can I acutally display the image?
Recrehal is offline
Reply With Quote
View Public Profile Visit Recrehal's homepage!
 
 
Register now for full access!
Old 05-01-2005, 08:18 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
The snippet below was done ages and ages ago, i know it works because i've used it for ages. The comments before the code explain how to set out the function properties.

Give it a try, it should work, you'll need to change the first line of actual code to what you want it to be. You may find it won't display the picture on the page (it should though) but it will have created the resized image in your file system.

PHP Code:
<?php
/* resizeToFile resizes a picture and writes it to the harddisk
 *  
 * $sourcefile = the filename of the picture that is going to be resized
 * $dest_x      = X-Size of the target picture in pixels
 * $dest_y      = Y-Size of the target picture in pixels
 * $targetfile = The name under which the resized picture will be stored
 * $jpegqual  = The Compression-Rate that is to be used 
 */
 

 
resizeToFile ("350x50.jpg""50""50""done.jpg""100");
 echo (
'<IMG SRC="done.jpg" BORDER=0>');
 exit();


function 
resizeToFile ($sourcefile$dest_x$dest_y$targetfile$jpegqual)
{
   

/* Get the dimensions of the source picture */
   
$picsize=getimagesize("$sourcefile");

   
$source_x  $picsize[0];
   
$source_y  $picsize[1];
   
$source_id imageCreateFromJPEG("$sourcefile");

/* Create a new image object (not neccessarily true colour) */
       
   
$target_id=imagecreatetruecolor($dest_x$dest_y);

/* Resize the original picture and copy it into the just created image
   object. Because of the lack of space I had to wrap the parameters to 
   several lines. I recommend putting them in one line in order keep your
   code clean and readable */
       

   
$target_pic=imagecopyresampled($target_id,$source_id,
                                   
0,0,0,0,
                                   
$dest_x,$dest_y,
                                   
$source_x,$source_y);

/* Create a jpeg with the quality of "$jpegqual" out of the
   image object "$target_pic".
   This will be saved as $targetfile */
 
   
imagejpeg ($target_id,"$targetfile",$jpegqual);
   
   
// Free up the memory and destroy original image
   
unlink($sourcefile);

   return 
true;


?>
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 05-02-2005, 04:16 AM
Recrehal's Avatar
Experienced Talker

Posts: 39
Location: Aachen, Germany
Trades: 0
Hm I see that this works, but I don't want to safe the file somewhere but rather output it directly. I use imagejpeg to create the raw stream so I somehow need to get the browser to understand that this is a jpg and not plain text.
Recrehal is offline
Reply With Quote
View Public Profile Visit Recrehal's homepage!
 
Old 05-02-2005, 05:23 AM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
yeah... good luck.

Why don't you just change the function to delete the file once you've printed the HTML, you might want to use the sleep() function to give it some time to load though.

Why you gotta make it so difficult!?
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 05-02-2005, 01:24 PM
Recrehal's Avatar
Experienced Talker

Posts: 39
Location: Aachen, Germany
Trades: 0
Because a quick shot doesn't go well with the complex rest of the site ^_^

Okay so I want to create a gallery from a whole directory and therefore generate the thumbnails on the fly. If I delete the picture after a certain time, other users might access the file and find it deleted at half way (I guess that's what your sleep was good for in the first place).

I see your point, though. I think it would be way smarter to create the files once instead of each and every time a users wants to take a look at a gallery. The only thing I'd need to do then is check if a thumbnail exists and if it doesn't generate it.

Nevertheless, I still want to know if it would be possible to output the generated file directly. For little things like buttons that would be generated quickly but dynamically it would be nice to output the result directly without storing it to the HD. If there is no other way than to safe it and later delete it I'll willingly accept that, but I think there is a way.
Recrehal is offline
Reply With Quote
View Public Profile Visit Recrehal's homepage!
 
Old 05-02-2005, 02:41 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
Hmm, I see what you wanna do now. The only reserves I have about it is the fact that im not sure this is possible, because the browser see's the HTML image tag and ignores it until it's compiled the rest of the code, then downloads the image right at the end in order - downloads it as a physical thing. The only way i can think you would do it is actually call the script in the img src tag, and use a query string or something??

Also, you may have a problem creating this all the time, because once you have high volumes of traffic on your site, it's going to slow it right down - you'll be using the cache all the time, and the server might just poop out, and your provider will not be too happy!

I dunno, im not to sure it's a good idea, image's don't take up too much space, or bandwidth - not thumbnails anyway. I just think calling a function over and over for something like that is going to kill.
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to pictures with PHP?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23351 seconds with 12 queries