Adam Opalecký Adam Opalecký - 1 year ago 160
PHP Question

PHP - ImageCreateFromJPEG() return weird code

I am trying to create thumbnails that will be created by server and then outputted to HTML for user of website but it returns wierd code...
My function is:

function createthumbnail($n){
list($width, $height) = getimagesize($n);
$newheight = 100;
$ratio = 100/$height;
$newwidth = $width*$ratio;
$im = imagecreatetruecolor($newwidth, $newheight);
case "jpg":
$foto_r = imagecreatefromjpg($n);
case "png":
$foto_r = imagecreatefrompng($n);
case "bmp":
$foto_r = imagecreatefromwbmp($n);
$foto_r = imagecreatefromjpeg($n);
$im = imagecreatetruecolor($newwidth, $newheight);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
imagestring($im, 1, 5, 5, 'Error loading ' . $n, $tc);
imagecopyresampled($im, $foto_r, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($im, null, 100);
return $foto_r;

Here is example of the wierd code:
Wierd code example

Answer Source

If I see it right, then your script builds the html for your webpage and the thumbnail in one single run? Probably something like that?

<img src="<?php echo createthumbnail('path_to_the_image.jpeg'); ?>" alt="" />

The thumbnail has to be an extra file, so another php script that generates the image and the image headers.

<img src="generatethumbnail.php?f=path_to_the_image.jpeg" alt="" />

There is also a "dirty" solution to include the image code as base64 directly in your html code Embedding Base64 Images


<img src="data:image/jpeg;base64,<?php 
echo base64_encode(createthumbnail('path_to_the_image.jpeg')); 
?>" alt="" />

It is not supported by older browser, the images are not cached in the client that way and the loading time for your site increases drastically, because all the thumbnails would be loaded with the code

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download