Image appearing as binary text in browser

images php coding issues or problems here.

Moderators: macek, egami, gesf

Nehle
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Jan 21, 2003 3:13 pm

Postby Nehle » Tue Jan 21, 2003 3:23 pm

Simple,

imagepng(); outputs the data to the standard output buffer, when used in a a simple php file, the browser will only read the imagepng(); output and determine by the header that it is an image. When you have use it in a text however, it will have read the header as it being a text, and thus read the image as a textfile and not an image

Solve this by creating a PHP-script for accessing your files, such as this


Code: Select all


<?php
if(isset($_GET['img'])) {
   $img = $_GET['img'];
   image($img);
}
else return;

function image($filename) {
$path=/home/foo/bar/php //path to  the script, without trailing slash , I usually get it from a 'config.php'
   $file = $filename;
   if(!is_readable($file)) return;
   $type = getimagesize($file);
   if($type[2]!=3)  return;
              else {
   $src_img = imagecreatefrompng("$path/$file");
   imagepng($src_img);
   imagedestroy($src_img);
              return;


}
?>


now all you have to do is call the image script (image.php or whatever)
and add ?img=[path to file]


And no, there is no noticable difference between using php to create the thumbnail and showing pre-made thumbnails, unless you're on a slow server

EDIT: hmm...copy-paste is not well if you want nice indenting -_-

User avatar
pootergeist
New php-forum User
New php-forum User
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK
Contact:

Postby pootergeist » Wed Jan 29, 2003 7:19 am

Even though a small site will not drain too many resources by dynamically creating images at page load, I would strongly advise against doing such. It is better if you can create a file on the server and target that (either at image upload time, or through an admin backend).
Imagine you had 10 images of 100KB and 5000 hits per day. That would effect 10x100x5000 KB or processing per day (5 gig[ish]). Also note: until GD2.0.7 Thomas Boutel had some issues with server memory allocation and cleanup, so if you are using an older build, you should try to reduce the amount of times your scripts run.

All the GD image output functions have parameters for saving a local copy (second parameter on all save jpeg, which also has a compression parameter)

ImageJPEG($img_ref, 'path/filename.jpg', 80);
would save a copy of the output $img_ref as path/filename.jpg with a quality of 80%

User avatar
Redcircle
Moderator
Moderator
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA
Contact:

Postby Redcircle » Fri Jan 31, 2003 1:13 am

you can also try using a header call before the output

Header("Content-Type: image/png");


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest