473,657 Members | 2,492 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.Outpu tStream". 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 2724
Atli
5,058 Recognized Expert Expert
Hey.

I don't think there is an equivalent to "Response.Outpu tStream" 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
3644
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
2288
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 this regard. It is a serious problem for me. Thanks! Approach - I - Bitmap (screen capture method) ============================================= Can able to create the thumbnail image using Bitmap and BitBlt.
2
4577
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 scale it down. This is quite slow, since there is a lot of file IO going on, what I want to be able to do is open the image in the scaled down size so that I miss out the step of opening the file then dowsizing it. I have not been able to find...
6
4745
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 size of a thumbnail and set the sizemode to Stretch -- I get one thumbnail. I want to retrieve all the picture files (jpg, bmp) in a directory into an array list and then display this list as thumbnails on my form dynamically. So my question is...
8
20354
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: Start with original thumbnails & two empty sub directories STEP 2: Create smaller versions of the originals for one sub directory STEP 3: Create thumbnail version of the originals the other sub directory STEP 4: Create an index.html pointing to the...
2
3536
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 from video file in asp.net?????? please let me know if anyone has done this before
1
2899
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 from video file in asp.net?????? please let me know if anyone has done this before
2
1570
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. Below is my current test script. All images and php are in the same dir for testing. Thanks. <? exec("convert file.jpg -resize 100x75 new_file.jpg"); // display original photo if (file_exists('file.jpg')) { ?>
11
2937
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, having hyperlinks to the corresponding full size images. Can anybody point me to locations in MSDN or elsewhere giving the references to attach, the commands & objects for creating or opening the pages and possibly available classes? I have done...
3
4377
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 thumbnail, on both approaches it seems to use a lot of ram memory to keep that image displayed on the thumbnail and scrolling through many thumbnails can suck up some cpu time, what is the fastest approach to create several controls, thumbnail like...
0
8403
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8610
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6174
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.