473,503 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP create thumbnail

Frinavale
9,735 Recognized Expert Moderator Expert
Sorry for the newbie question but PHP is not my forte...

I am attempting to implement a script that will retrieve a file, create a thumbnail image, and write the thumbnail to the "Response.OutputStream". The thing is that I don't think that such a thing exists in PHP and so I'm a bit lost.

How do I write the file (as bytes) directly to the response?

This is what I have so far....
Expand|Select|Wrap|Line Numbers
  1. // load image and get image size
  2.       $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
  3.       $width = imagesx( $img );
  4.       $height = imagesy( $img );
  5.  
  6.       // calculate thumbnail size
  7.       $new_width = $thumbWidth;
  8.       $new_height = floor( $height * ( $thumbWidth / $width ) );
  9.  
  10.       // create a new temporary image
  11.       $tmp_img = imagecreatetruecolor( $new_width, $new_height );
  12.  
  13.       // copy and resize old image into new image
  14.       imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
  15.  
  16.       // save thumbnail into a file
  17.       //imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
  18. //>>> Here I want to save the file to the to the output stream <<<
  19.  
Any guidance on the issue would be appreciated.

Thanks

-Frinny
Mar 31 '10 #1
4 2719
Atli
5,058 Recognized Expert Expert
Hey.

I don't think there is an equivalent to "Response.OutputStream" in PHP. There is no need for it.

Everything PHP prints goes directly into the output stream, and it prints binary data just as it does strings. (Technically it would be the other way around, but the result is the same.)

That is, if you want to have PHP read and print an image from the file-system, you simply do:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header("Content-type: image/png");
  3. $data = file_get_contents("some_image.png");
  4. echo $data;
  5. ?>
The $data would be a string of bytes, and the echo call would just add that to the output buffer. Text is also treated as a string of bytes, so there really isn't any difference between how the two are treated.

In the case of GD images, you can use the GD "image*" functions (imagepng, imagejpeg, etc...) to create the image in the format you want and add it to the output. - Note, if you provide a file path in the second parameter, it saves it there. If you omit it, the data goes directly into the output buffer.
Mar 31 '10 #2
Frinavale
9,735 Recognized Expert Moderator Expert
Ahh, thank you so much Atli.
I'll have to check this out later tonight :)
Mar 31 '10 #3
Frinavale
9,735 Recognized Expert Moderator Expert
**Huge Sigh of Disappointment*

Apparently I can't do what I want to do because the (CMS) PHP framework I'm developing on top of is preventing people from accessing my script directly.

This means that I can't create the thumbnail component to be used in my application.

I'm so disappointed right now.

I guess I'm going to have to try and resize the images using JavaScript instead (so that they aren't distorted by the <img> tag).


Thanks again for your help.

-Frinny
Apr 2 '10 #4
Atli
5,058 Recognized Expert Expert
There is one thing you could try.

If you have access to the index.php of your CMS (assuming all requests are routed through a central point?), then you could add ob_start() at the very top, which turns on Output Buffering, preventing anything from being printed.

Then you can create a normal request via your CMS, and when it reaches the PHP code for that request you can issue a ob_end_clean(), print your thumb, and exit the code.

For example, in a MVC app...
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* file: index.php */
  3.  
  4. // Start the output buffering.
  5. ob_start();
  6.  
  7. // Kick of the MVC app.
  8. ($ctrl = @$_GET['ctrl']) or $ctrl = "Default";
  9. ($action = @$_GET['action']) or "default";
  10.  
  11. $ctrl = ucfirst($ctrl) . "Controller";
  12. require $ctrl_class . ".php";
  13.  
  14. $inst = new $ctrl_class();
  15. $inst->$action();
  16.  
  17. // Mandatory end-flush of the Output Buffer.
  18. ob_end_flush();
  19. ?>
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* file ThumbController.php */
  3.  
  4. class ThumbController {
  5.     function create() {
  6.         // Disgard and stop the Output Buffer
  7.         ob_end_clean();
  8.  
  9.         // Create your thumb
  10.         // ... etc
  11.  
  12.         // Print
  13.         header('Content-type: image/jpeg');
  14.         imagejpeg($myGdImage);
  15.  
  16.         // Exit the application, leaving on the image data in the
  17.         // response.
  18.         exit;
  19.     }
  20. }
  21. ?>
Now you can just call index.php?ctrl=thumb&action=create and it would display your thumb.
Apr 6 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3632
by: Phil Powell | last post by:
PHP 4.3.2 with --enable-exif I have the following class: <?php class ThumbGenerator extends MethodGeneratorForActionPerformer { function ThumbGenerator() { // CONSTRUCTOR
0
2276
by: Balu Ramachandran | last post by:
How can I create the Thumbnail view from the form? In the following approach I tried but Iams facing some limitations to get the thumbnail image of the form alone. Plaese any one can help in...
2
4569
by: Mark R. Dawson | last post by:
Hi all, I have a directory full of images (most over 2MB in size) I was to show each image as a thumbnail on a form, however in order to create a thumbnail I have to open the complete image then...
6
4729
by: Rich | last post by:
Hello, I want to simulate the dynamic thumbnail display of Windows Explorer (winxp) on a form or pannel container. If I place a picture box on my container form/pannel and dimension it to the...
8
20327
by: barb | last post by:
So that the world at large benefits from our efforts, here is one fully documented way to use Windows Irfanview freeware to create thumbnail web galleries (http://www.irfanview.com). STEP 1:...
2
3529
by: Mahesh K. Anajni | last post by:
hi i am working on blog where member can upload his/her video. when member's viewed his blog it should show the thumbnail of video uploaded by him. so the problem is how to create a thumbnail...
1
2883
by: Mahesh K. Anajni | last post by:
hi i am working on blog where member can upload his/her video. when member's viewed his blog it should show the thumbnail of video uploaded by him. so the problem is how to create a thumbnail...
2
1559
by: empiresolutions | last post by:
I've installed Imagemajick. phpinfo() shows it and GDlib 2.0 installed properly. I have tried multiple tests to get it to work and all have failed. Can someone please tell me what i am doing wrong....
11
2911
by: =?Utf-8?B?UGV0ZXIgSw==?= | last post by:
I am working with Visual Studio or alternately with Expression Web. I need to create about 50 aspx pages with about 1200 thumbnali images, typically arranged in three to four groups per page,...
3
4358
by: theplague | last post by:
Hi guys, I've tried to create an user control that contains a picture box and have also created a similar control that paints an image inside a panel control with a border to make it look like a...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.