Here's a jpg: http://i.stack.imgur.com/PIFN0.jpg
Let's say I'd like this rendered from /img.php?file_name=PIFN0.jpg
Here's how I'm trying to make this work:
/sample.php
<p>Here's my image:</p>
<img src="/img.php?file_name=PIFN0.jpg">
/img.php
<?php
$url = 'http://i.stack.imgur.com/' . $_GET['file_name'];
header('Content-type: image/jpeg');
imagejpeg($url);
?>
I would expect /sample.php to show the image. But this doesn't work. All I get is a broken image. What am I doing wrong?
Use imagecreatefromjpeg:
<?php
$url = 'http://i.stack.imgur.com/' . $_GET['file_name'];
header('Content-type: image/jpeg');
imagejpeg(imagecreatefromjpeg($url));
?>
Reference: http://php.net/manual/en/function.imagecreatefromjpeg.php
Here is an working example:
<?php
function img_create($filename, $mime_type)
{
$content = file_get_contents($filename);
$base64 = base64_encode($content);
return ('data:' . $mime_type . ';base64,' . $base64);
}
?>
<img src="<?php print img_create('http://tuxpaint.org/stamps/stamps/animals/birds/cartoon/tux.png','image/png'); ?>" alt="random logo" />
No need to use the GD functions:
<?php
$url = 'http://i.stack.imgur.com/' . $_GET['file_name'];
header('Content-type: image/jpeg');
readfile($url);
?>
<?php
header("Content-Type: image/jpeg");
$url = "http://i.stack.imgur.com/PIFN0.jpg";
$imgContents = file_get_contents($url);
$image = #imagecreatefromstring($imgContents);
imagejpeg($image);
?>
header('Content-type: image/jpeg');
readfile($_GET['id']);
but in the new server, only can read the local images, the remote images is error.
Related
I want to hide image path from website. To do that first I did this:
<?php
// get image path
$path = 'images/profiles/uploads/' . $list['thumbnail'];
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
?>
But this is very slow, So I try to implement like this, but code is not working:
download.php:
function setImgDownload($imagePath) {
$image = imagecreatefromjpeg($imagePath);
header('Content-Type: image/jpeg');
imagejpeg($image);
}
mypage.php:
<?php include('download.php'); ?>
<img src="<?php setImgDownload('images/Chrysanthemum.jpg') ?>" width="300"/>
If I put function and call function at the same page it's working:
function setImgDownload($imagePath) {
$image = imagecreatefromjpeg($imagePath);
header('Content-Type: image/jpeg');
imagejpeg($image);
}
setImgDownload('images/Chrysanthemum.jpg');
How to keep function in separate page?
Try changing your mypage.php to
<?php
include('download.php');
echo "<img src='".setImgDownload('images/Chrysanthemum.jpg')."' width='300'/>"; // calling function inside php
?>
Here, instead creating the HTML element outside php, put it inside php using echo and just escape while calling the php function. This should give you what you need.
<?php
$dir_pdf = "http://example.com/test/uploads/test_file.pdf";
$dir_thumbnail = "http://example.com/test/uploads/test_file.jpg"
$im = new Imagick();
$im->readImage($dir_pdf);
header("Content-Type: image/" . $im->getImageFormat());
echo $im;
?>
In folder "uploads" i have a pdf file
My server installed Imagick & Ghostscript
Why this code can't run :)
Thank you .
<?php
$im = new Imagick("uploads/test_file.pdf[0]");
$im->setImageFormat("jpg");
$type=$im->getFormat();
header("Content-type: $type");
echo $im->getimageblob();
?>
this is my code :)
I put test_file.pdf in folder uploads
it work ^^!
I generate the canvas and pass it to php so:
$('body').on('click','#save_image',function(){
html2canvas($('.myImage'), {
onrendered: function(canvas) {
//$('.imageHolder').html(canvas);
var dataURL = canvas.toDataURL("image/png");
// $('.imageHolder').append('<img src="'+dataURL+'" />');
$('.imageHolder').html('Generating..');
$.post('image.php',{image: dataURL},function(data){
$('.imageHolder').html(data);
});
}
});
});
image.php:
<?
$image = $_POST['image'];
echo "<img src='$image' alt='image' />";
$decoded = str_replace('data:image/png;base64,','',$image);
$name = time();
file_put_contents("/home/toni005/public_html/toniweb.us/div2img/" . $name . ".png", $decoded);
echo '<p>Download</p>';
?>
download.php:
<? $file = $_GET['img'];
header('Content-Description: File Transfer');
header("Content-type: image/jpg");
header("Content-disposition: attachment; filename= ".$file."");
readfile($file);
?>
The thing is that the image is generated, when I click the download link the download is forzed but the image cannot be opened (seems to be corrupted)
What am I missing?
It can be tested here: http://toniweb.us/div2img/
You should probably base64_decode() the data URL. It even says it in the URL itself: data:image/png;base64,...
$decoded = base64_decode(str_replace('data:image/png;base64,', '', $image));
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);
How can I put image from the memory to the browser, without saving.
For example:
function getImage()
{
$imageFile = imagecreatefromjpeg('Map.jpg');
$imageObject = imagecreatefrompng('image2.png');
imagealphablending($imageFile, true);
imagecopy(....);
$ret = array($imageFile, $imageObject) ;
return $ret
}
<?php $ret = getImage(); ?>
<img src = <?php $ret[0];? alt=''>
Is this possible, without saving?
Yes,
Just try imagejpeg($img);
and put into <img src= path to the PHP script which render the image
See sample at: http://php.net/manual/en/function.imagecreatefromjpeg.php
Maybe if you would code your image to base64 and use it like that, it would work:
<?php
$img_str = base64_encode($imgbinary);
echo '<img src="data:image/jpg;base64,'.$img_str.'" />';
?>
HTML:
<img src="data:image/jpg;base64,R0lGODlhCgAKAJEAAAAAAP///81Wv81WvyH5BAEAAAMALAAAAAAKAAoAAAIUjIViq+x7QpunwXoZ lXFu/mjIUgAAOw==" alt="image" />
I infered that you want to do this in one request.
You should have a script which sends proper headers and then it should be recognized as an image by the browser. Something like:
<?php
ob_start();
// assuming you have image data in $imagedata
$length = strlen($imagedata);
header('Last-Modified: '.date('r'));
header('Accept-Ranges: bytes');
header('Content-Length: '.$length);
header('Content-Type: image/jpeg');
print($imagedata);
ob_end_flush();
?>