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.... -
// load image and get image size
-
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
-
$width = imagesx( $img );
-
$height = imagesy( $img );
-
-
// calculate thumbnail size
-
$new_width = $thumbWidth;
-
$new_height = floor( $height * ( $thumbWidth / $width ) );
-
-
// create a new temporary image
-
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
-
-
// copy and resize old image into new image
-
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
-
-
// save thumbnail into a file
-
//imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
-
//>>> Here I want to save the file to the to the output stream <<<
-
Any guidance on the issue would be appreciated.
Thanks
-Frinny
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: - <?php
-
header("Content-type: image/png");
-
$data = file_get_contents("some_image.png");
-
echo $data;
-
?>
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.
Frinavale 9,735
Recognized Expert Moderator Expert
Ahh, thank you so much Atli.
I'll have to check this out later tonight :)
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
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... - <?php
-
/* file: index.php */
-
-
// Start the output buffering.
-
ob_start();
-
-
// Kick of the MVC app.
-
($ctrl = @$_GET['ctrl']) or $ctrl = "Default";
-
($action = @$_GET['action']) or "default";
-
-
$ctrl = ucfirst($ctrl) . "Controller";
-
require $ctrl_class . ".php";
-
-
$inst = new $ctrl_class();
-
$inst->$action();
-
-
// Mandatory end-flush of the Output Buffer.
-
ob_end_flush();
-
?>
- <?php
-
/* file ThumbController.php */
-
-
class ThumbController {
-
function create() {
-
// Disgard and stop the Output Buffer
-
ob_end_clean();
-
-
// Create your thumb
-
// ... etc
-
-
// Print
-
header('Content-type: image/jpeg');
-
imagejpeg($myGdImage);
-
-
// Exit the application, leaving on the image data in the
-
// response.
-
exit;
-
}
-
}
-
?>
Now you can just call index.php?ctrl=thumb&action=create and it would display your thumb.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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.
|
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...
|
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...
|
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...
| |
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
|
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
|
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'))
{
?>
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |