PHP output image issues - php

if (file_exists($path)) {
$fileContent = id3_getImage($path);
$fileMime = id3_getMimeOfImage($path); // is set to image/jpeg
if ($fileMime != "") {
header('Content-Type: '.$fileMime);
print($fileContent);
die;
}
}
So the above code does not work in the browser, however when I make a image with image
$img = imagecreatefromstring($fileContent);
imagejpeg($img, 'test.jpg');die;
the image is created and I can view it on my computer. So I have no clue what I am doing wrong :(
*Could it be a setting in the apache conf? I honestly have no clue on this one

header('Content-Type: image/jpeg');
// Output the image
imagejpeg($img);
You need send header before. But if you want to create the image and show, you need create, and read this for the browser.
readfile($filename);
You can read the man here:
http://php.net/manual/en/function.readfile.php

I use smartReadFile.php from Google Groups to display any type of file.
https://jplayer.googlegroups.com/attach/f308294ddea52f6c/smartReadFile.php?view=1&part=4
It's really smart. =)
<?php
require "smartReadFile.php";
$dir = 'images/';
$filename = 'someimage' . '.jpg';
$location = $dir.$filename;
smartReadFile($location, $filename);
?>

This one is working for me when reading from DB
$image = imagecreatefromstring([path/DBfiels]);
header('content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);

Related

Transferring and displaying image from remote location

I need to read an image (based on HTTP) on an SSL connection. This script reads the image location and encodes/decodes it, then transfers the file contents and returns the image. However, I keep getting the error cannot be displayed because it contains errors.
Here is my code:
<?php
$image = 'http://www.teleovronnaz.ch/webcam/ovronnazweb.jpg';
$info = getimagesize($image);
$strFileExt = image_type_to_extension($info[2]);
if($strFileExt == '.jpg' or $strFileExt == '.jpeg'){
header('Content-Type: image/jpeg');
}elseif($strFileExt == '.png'){
header('Content-Type: image/png');
}elseif($strFileExt == '.gif'){
header('Content-Type: image/gif');
}else{
die('not supported');
}
if($strFile != ''){
$cache_ends = 60*60*24*365;
header("Pragma: public");
header("Cache-Control: maxage=". $cache_ends);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache_ends).' GMT');
$img_safe = file_get_contents($strFile);
echo $img_safe;
}
exit;
?>
This worked for me.
<?php
$url = 'http://www.teleovronnaz.ch/webcam/ovronnazweb.jpg';
header('Content-type: image/jpeg');
readfile($url);
?>
I don't know why you do it so complicated. I just stripped your cache handling, but really
<?PHP
$image = 'http://www.teleovronnaz.ch/webcam/ovronnazweb.jpg';
$imageContent = file_get_contents($image);
header("Content-Type: image/jpeg");
echo $imageContent;
die(1);
?>
Is enough to display the image. No base64 or additional stuff needed. And if the url is static, you don't even need the distinction of the file extension. You stated an image - that's singular. So I'm guessing that's the only use case here.
I just took some more time to explain a few things:
read an image (based on HTTP) on an SSL connection
If it's HTTP, there is no SSL. That's what HTTPS is for.
This script reads the image location and encodes/decodes it,
I don't know what you think, but it is not. It is base64_encoding the URL to directly decode it again into another variable. That's like doing the following: 0 is your $image - then you make $image+1 (base64_encode) - which will result in 1 - then you do $image-1 (base64_decode) which will result in 0 again.

Image save in specific folder and return that image path in PHP

I am trying to generate a barcode and save into one folder, and after save I want path of that image. Here is the code:
header('Content-type: image/jpeg');
imagejpeg($im);
echo $save = "temp/". strtolower($code) .".jpeg";
// $save1 = $_SERVER['DOCUMENT_ROOT'] . "/sigs/" . strtolower($name) . ".png";
$tt= imagejpeg($im, $save);
imagedestroy($im);
The image saves successfully, but path does not return or even print - what might be wrong?
but path not return or even not print please help me
Of course it does not. It prints 1 as expected. What you got wrong is this line:
echo $save = "temp/". strtolower($code) .".jpeg";
it should be:
$save = "temp/". strtolower($code) .".jpeg";
echo $save;
instead.

PHP - Script using imagepng won't save file and then display it

I'm trying to load an image, edit it, save it and then display it from a script that is called within IMG tags. The script works if I want to just display the image and it does save the image. But it won't save it and then display it. Does anyone know what I'm doing wrong? Any help would be greatly appreciated.
<?php
header('Content-Type: image/png');
$file_location = "test.png";
if (file_exists($file_location)) {
$img_display = #imagecreatefrompng($file_location);
// This section of code removed as doesn't affect result
imagepng($img_display, $file_location);
chmod($file_location, 0777);
imagepng($img_display);
imagedestroy($img_display);
}
?>
Try this and checks if your folder has permission to save the image. chmod 777 on it for sure.
<?php
header('Content-Type: image/png');
$file_location = "test.png";
if (file_exists($file_location)) {
$img_display = imagecreatefrompng($file_location); // Create PNG image
$filename = $file_location + time(); // Change the original name
imagepng($img_display, $filename); // Saves it with another name
imagepng($filename); // Sends it to the browser
imagedestroy($img_display); // Destroy the first image
imagedestroy($filename); // Destroy the second image
}
Try this:
ob_start();
imagepng($img_display);
$contents = ob_get_clean();
file_put_contents($file_location, $contents);
echo $contents;
imagedestroy($img_display);

Other non-image related code before and after header no longer work

I am trying to use header('Content-type: image/jpeg') to edit and display jpeg photos as follows.
header('Content-type: image/jpeg')
$filename = 'aaa.jpg';
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_CONTRAST,50);
imagejpeg($im);
imagedestroy($im);
In the same file,I also have other simple codes like $abc = $_POST['abc'].
After I put the header, code before the header and code after image destroy($im) no longer work. And when I put any code such as $_post['abc'] before the header, both header and code doesn't work. All codes were fine before I included header and code to manipulate and output image. It is my first time using header('Content-type: image/jpeg') and I cannot find the answer after trying for so long. Please help. Thank you.
If you want to output html page, do not send image header. But instead, at first output the transformed image to a file on your server and add the <img> or <a>nchor tag in your html page:
<html><body>
<?php
$output_dir = 'images';
if (!file_exists($output_dir)) {
mkdir($output_dir, 0777);
}
$filename = 'aaa.jpg';
$filename2 = 'aaa2.jpg';
if (!file_exists($filename)) {
echo 'Input image not exists!'; exit;
}
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_CONTRAST, 50);
imagejpeg($im, $output_dir.'/'.$filename2);
imagedestroy($im);
echo 'Original image:<br/><img src="'.$filename.'" /><br/>';
echo 'Transformed image:<br/><img src="'.$output_dir.'/'.$filename2.'" />';
?>
</body></html>
That image header send in case, you want to output it as standalone image. For more examples have a look at php.net.

uploading an image from flash using php more specifically HTTP_RAW_POST_DATA?

I currently have it working so it displays a dialogue box to save the image on your computer:
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
// get bytearray
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
// add headers for download dialog-box
header('Content-Type: image/jpeg');
header("Content-Disposition: attachment; filename=".$_GET['name']);
echo $jpg;
}
just wondered if there is any way to put the file straight into a directory/file without the need of a dialogue box?
like an uploader?
No, there is not.
Just Read the content of the page and save in a file using fopen , fwrite e.t.c.
if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){
// get bytearray
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
// add headers for download dialog-box
ob_start();
header('Content-Type: image/jpeg');
echo $jpg;
$image=ob_get_clean();
//and here write it into file
}
OR following is my code you can remove unneccessary things that are not useful for you
if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
$im = $GLOBALS["HTTP_RAW_POST_DATA"];
$filename=$_GET['name'];
$fullFilePath='files/'.$filename;
$handle=fopen($fullFilePath,"w");
fwrite($handle,$im);
fclose($handle);
$returnVars = array();
$returnVars['write'] = "yes";
$returnString = http_build_query($returnVars);
//send variables back to Flash
echo $returnString;
}

Categories