outputing image part 2

This is a discussion on outputing image part 2 within the PHP General forums, part of the PHP Programming Forums category; I have manually put in the url my display_image.php page to debug as sugested and all I get is ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-15-2007
Ross
 
Posts: n/a
Default outputing image part 2


I have manually put in the url my display_image.php page to debug as
sugested and all I get is the URL of the display_image.php page output on
the screen.

This is what I see
http://xxxxxxxxxxxxxxx.co.uk/common/display_image.php


this is the display_image.php file:

$img_url="http://www.xxxxxxxxxxxxx.co.uk/images/ENapt63377/4.jpg";

$image = imagecreatefromjpeg($img_url);
if ($image === false) { exit; }

// Get original width and height
echo $width = imagesx($image);
echo $height = imagesy($image);

// New width and height
$new_width = 200;
$new_height = 150;

// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width,
$new_height, $width, $height);

// Display resized image

header('Content-type: image/jpeg');
imagejpeg($image_resized);
exit();
Reply With Quote
  #2 (permalink)  
Old 06-15-2007
Stut
 
Posts: n/a
Default Re: [PHP] outputing image part 2

Ross wrote:
> I have manually put in the url my display_image.php page to debug as
> sugested and all I get is the URL of the display_image.php page output on
> the screen.
>
> This is what I see
> http://xxxxxxxxxxxxxxx.co.uk/common/display_image.php
>
>
> this is the display_image.php file:
>
> $img_url="http://www.xxxxxxxxxxxxx.co.uk/images/ENapt63377/4.jpg";


This should almost certainly be a filename not a URL.

> $image = imagecreatefromjpeg($img_url);
> if ($image === false) { exit; }
>
> // Get original width and height
> echo $width = imagesx($image);
> echo $height = imagesy($image);
>
> // New width and height
> $new_width = 200;
> $new_height = 150;
>
> // Resample
> $image_resized = imagecreatetruecolor($new_width, $new_height);
> imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width,
> $new_height, $width, $height);
>
> // Display resized image
>
> header('Content-type: image/jpeg');


Comment out this header line. Chances are you're getting an error
message, but because you're telling the browser it's an image the
browser is trying to display it as an image. Because it can't, it's
displaying the default alt tag which is the URL (assuming you're using
Firefox which is the only browser I know of to do this).

> imagejpeg($image_resized);
> exit();


-Stut
Reply With Quote
  #3 (permalink)  
Old 06-15-2007
Ross
 
Posts: n/a
Default Re: outputing image part 2

Thanks Stut we are getting somewhere.

Removing the header gives the full binary output of the file in IE and FF. I
also removes the echos and have the resized image showing in the browser. So
far so good. I know it works.


Now back to sending the URL.

I have this

$img_url="http://www.xxxxxxx.co.uk/images/ENbb24469/room1.JPG";
echo "<img src=\"common/display_image.php?img_url='$img_url'\" width=\"200\"
height=\"100\" />";


and on the display image page I have:

$img_url= $_GET['img_url'];
$image = imagecreatefromjpeg($img_url);
if ($image === false) { exit; }

/// rest of the resize code goes here.....


This still does not work but if I plug the url in manually at the top of
display_image.php it works, this suggests it is not being sent properly.

Many thanks,


R.
Reply With Quote
  #4 (permalink)  
Old 06-15-2007
Richard Davey
 
Posts: n/a
Default Re: [PHP] Re: outputing image part 2

Hi Ross,

Friday, June 15, 2007, 10:00:53 PM, you wrote:

> I have this


> $img_url="http://www.xxxxxxx.co.uk/images/ENbb24469/room1.JPG";
> echo "<img src=\"common/display_image.php?img_url='$img_url'\" width=\"200\"

height=\"100\" />>";

> and on the display image page I have:


> $img_url= $_GET['img_url'];
> $image = imagecreatefromjpeg($img_url);
> if ($image === false) { exit; }


> /// rest of the resize code goes here.....


> This still does not work but if I plug the url in manually at the top of
> display_image.php it works, this suggests it is not being sent properly.


You are sending a complete URL to your image, not a path. This means
that if your installation of PHP does NOT have the ability to open
files from URLs (allow_url_fopen) then it won't be able to open the
image from the location you gave it.

If you want to load in images from other web sites then make sure
allow_url_fopen is enabled in PHP. If you don't want to do this, then
don't pass in a URL, pass in the *path* to the image instead.

Cheers,

Rich
--
Zend Certified Engineer
http://www.corephp.co.uk

"Never trust a computer you can't throw out of a window"
Reply With Quote
  #5 (permalink)  
Old 06-15-2007
Stut
 
Posts: n/a
Default Re: [PHP] Re: outputing image part 2

Ross wrote:
> Thanks Stut we are getting somewhere.
>
> Removing the header gives the full binary output of the file in IE and FF. I
> also removes the echos and have the resized image showing in the browser. So
> far so good. I know it works.
>
>
> Now back to sending the URL.
>
> I have this
>
> $img_url="http://www.xxxxxxx.co.uk/images/ENbb24469/room1.JPG";
> echo "<img src=\"common/display_image.php?img_url='$img_url'\" width=\"200\"
> height=\"100\" />";
>
>
> and on the display image page I have:
>
> $img_url= $_GET['img_url'];
> $image = imagecreatefromjpeg($img_url);
> if ($image === false) { exit; }
>
> /// rest of the resize code goes here.....
>
>
> This still does not work but if I plug the url in manually at the top of
> display_image.php it works, this suggests it is not being sent properly.


OK, your problem here is the single quotes in the img src around $img_url.

However, I question what you are actually doing. Is the image on the
same machine as this script? If so you should be using a filename not
the URL. Also, are you sure you're really saving anything by resizing
the image on your server like this? You should consider generating the
smaller version using a cron job and linking to it directly. Resizing
images on the fly is one of the quickest ways to destroy the scalability
of a website.

-Stut
Reply With Quote
  #6 (permalink)  
Old 06-15-2007
Daniel Brown
 
Posts: n/a
Default Re: [PHP] Re: outputing image part 2

On 6/15/07, Stut <stuttle@gmail.com> wrote:
> OK, your problem here is the single quotes in the img src around $img_url.


Stut's right. You should change:
echo "<img src=\"common/display_image.php?img_url='$img_url'\"
width=\"200\" height=\"100\" />";
.... to:
echo "<img
src=\"common/display_image.php?img_url=".$img_url."\" width=\"200\"
height=\"100\" />";

He's also right about the dynamic resizing of images causing a
serious strain on the server. Each time it has to do that, it uses up
a significant amount of resources for a seemingly quick and small
operation. Multiply that by n number of simultaneous accesses and you
can see the exponential - and potentially devastating - "pitfallic"
results.

--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107
Reply With Quote
  #7 (permalink)  
Old 06-15-2007
Ross
 
Posts: n/a
Default Re: [PHP] Re: outputing image part 2

I am taking your advice.


I am going to rezize any image greater than 400px wide prior to upload. I
have to combine my upload script

for ($i=0; $i <$total; $i++) {

$fileName = $_FILES['userfile']['name'][$i];
$tmpName = $_FILES['userfile']['tmp_name'][$i];
$fileSize = $_FILES['userfile']['size'][$i];
$fileType = $_FILES['userfile']['type'][$i];

$position=$i+1;

$img_url = "images/$customer_id/". basename(
$_FILES['userfile']['name'][$i]);

if(move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $img_url)) {
chmod('images/'.$customer_id, 0777);

chmod($img_url, 0777);

}


with my resize script


$image = imagecreatefromjpeg($img_url);
if ($image === false) { exit; }

// Get original width and height
$width = imagesx($image);
$height = imagesy($image);

// New width and height
$new_width = 200;
$new_height = 150;

// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width,
$new_height, $width, $height);


Any ideas how to save the imagecopyresampled() to the folder?

T
Reply With Quote
  #8 (permalink)  
Old 06-15-2007
Richard Davey
 
Posts: n/a
Default Re[2]: [PHP] Re: outputing image part 2

Hi Ross,

Friday, June 15, 2007, 10:40:37 PM, you wrote:

> Any ideas how to save the imagecopyresampled() to the folder?


Call imagepng (or imagejpeg or whatever) and pass it a filename to
save the image instead of output it. Check the help files for examples.

Cheers,

Rich
--
Zend Certified Engineer
http://www.corephp.co.uk

"Never trust a computer you can't throw out of a window"
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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



All times are GMT +1. The time now is 12:41 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0