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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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(); |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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" |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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" |