I need to embed http image in my https website, because if simply use <img src="http://www.sof.com/abc.jpg"/> will not show in https website.
After that, I was search some topic of this function, I found that something like <img src="https://www.some.com/image.php?url=http://www.sof.com/abc.jpg" /> can be show in https
So now I was genarate 2 source. 1 is the image file: http://www.website1.com/abc.png,
and the https web adress is https://fb.ccc.com
after that I create the image.php code was below:
<?
$strFile = base64_decode(#$_GET['url']);
$strFileExt = end(explode('.' , $strFile));
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_expire = 60*60*24*365;
header("Pragma: public");
header("Cache-Control: maxage=". $cache_expire);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache_expire).' GMT');
}
exit;
?>
if I request the link: https://fb.ccc.com/image.php?url=http://www.website1.com/abc.png , then the page will be respone not supported
I was also try $strFile = $_GET['url']; but it only show blank.
if I try $strFile = var_dump(#$_GET['url']); and the page will respone string(31) "http://www.website1.com/abc.png" not supported
so someone can help to find out what is my script problem?
thank you very much!
Related
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.
I'm trying to show a video on my Mac and I phone but I can't get it to work. My videos are served by php, I've been reading it has to do with range headers not being sent but I can't figure out how to do it correctly.
Heres what I have so far:
HTML:
<video controls="true" autoplay muted loop>
<source src="http://my_site.com/video/394934" type="video/mp4">
</video>
PHP:
// Get file GUID
$file_guid = (int) get_input('file_guid', 0);
// Get file thumbnail size
$size = get_input('size', 'small');
$file = get_entity($file_guid);
if (!elgg_instanceof($file, 'object', 'file')) {
exit;
}
$simpletype = $file->simpletype;
if ($simpletype == "image") {
// Get file thumbnail
switch ($size) {
case "small":
$thumbfile = $file->thumbnail;
break;
case "medium":
$thumbfile = $file->smallthumb;
break;
case "large":
default:
$thumbfile = $file->largethumb;
break;
}
// Grab the file
if ($thumbfile && !empty($thumbfile)) {
$readfile = new ElggFile();
$readfile->owner_guid = $file->owner_guid;
$readfile->setFilename($thumbfile);
$mime = $file->getMimeType();
$contents = $readfile->grabFile();
// caching images for 10 days
header("Content-type: $mime");
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+10 days")), true);
header("Pragma: public", true);
header("Cache-Control: public", true);
header("Content-Length: " . strlen($contents));
echo $contents;
exit;
}
}
if it is in fact a range headers issue, there's a github project with a code example tackling this issue for Safari/Mac:
https://gist.github.com/codler/3906826
When we output images via PHP with image_jpeg or file_get_contents it takes more than twice as long as when we use straight links to the jpg files.
These files are about 180kb.
With our thumbnails (4kb images) there's not much of a time difference between the link and output via PHP.
Anyone know of a why PHP output is slower with larger files and a way to fix it?
All I can think of is that it is being parsed twice when parsed through PHP, instead of directly sending it to the client. Because file_get_contents does what it says, it reads the contents, then sends it to the client. I could be wrong though.
There is different between image_jpeg and file_get_contents. The first is a gd function that creates a jpeg and this take time. The second just reads data from a file.
The problem is how you output it to the browser. If you don't take appropriate measures, content is never cached, so the browser has to download it every time. Static images are always cached by the browser and after the first load, takes almost no time (just a HEAD request).
Try this code:
function CachedFileResponse($thefile,$nocache=0) {
if (!file_exists($thefile)) {
error_log('cache error: file not found: '.$thefile);
header('HTTP/1.0 404 Not Found',true,404);
} else {
$lastmodified=gmdate('D, d M Y H:i:s \G\M\T', filemtime($thefile));
$etag = '"'.md5($lastmodified.filesize($thefile).$thefile).'"';
header('ETag: '.$etag);
header('Last-Modified: '.$lastmodified);
header('Cache-Control: max-age=3600');
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time()+86400));
$ext=strtolower(substr($thefile,strrpos($thefile,'.')+1));
$fname=substr($thefile,strrpos($thefile,'/')+1);
if ($ext=='jpg' || $ext=='jpeg') {
header('Content-Type: image/jpeg');
} elseif ($ext=='gif') {
header('Content-Type: image/gif');
} elseif ($ext=='png') {
header('Content-Type: image/png');
} else {
header('Content-Type: application/binary');
}
header('Content-Length: ' . filesize($thefile));
header('Content-Disposition: filename="'.$fname.'"');
$ifmodifiedsince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : false;
$ifnonematch = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : false;
if ($nocache || (!$ifmodifiedsince && !$ifnonematch) ||
($ifnonematch && $ifnonematch != $etag) ||
($ifmodifiedsince && $ifmodifiedsince != $lastmodified)) {
error_log('cache miss: '.$thefile);
$fp = fopen($thefile, 'rb');
fpassthru($fp);
fclose($fp);
} else {
error_log('cache hit: '.$thefile);
header("HTTP/1.0 304 Not Modified",true,304);
}
}
}
I am building a php script to write texts over an background image.
I used GD functions like imagecopy(), imagejpeg(), imagedestroy() to merge save text image and the background image. Everything is working perfectly. Upon form submit, the new image will be saved in the same file name of the background image and so on page reload, the edited image is not showing on the browser. It needs me to refresh the page using ctrl + F5(on windows) to load the edited image. Can anyone help me how to clear that cache?
just add ?v=something to background path everytime you edit your background image, it will force refresh
To handle image caching properly, you can write a rules either in your apache configuration or htaccess... or you can create simple "image provider", something like...
public function img($imgfile = '')
{
$imgfile = $_GET['q'];
$age = 60*60*24*31;
$file = $_SERVER['DOCUMENT_ROOT'].'/'.$imgfile;
if ( ! file_exists($file))
{
header('HTTP/1.0 404 Not Found');
}
else
{
$last_modified = filemtime($file);
// Check for cached version
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) OR isset($_SERVER['HTTP_IF_NONE_MATCH']))
{
if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == gmdate('D, d M Y H:i:s \G\M\T', $last_modified))
{
header('HTTP/1.0 304 Not Modified');
exit;
}
}
if(strpos($imgfile,'.png') !== FALSE)
{
Header('Content-Type: image/png');
}
elseif(strpos($imgfile,'.jpg') !== FALSE || strpos($img_file,'.jpeg') !== FALSE)
{
Header('Content-Type: image/jpg');
}
elseif(strpos($img_file,'.gif') !== FALSE)
{
Header('Content-Type: image/gif')
}
Header('Last-Modified : '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified));
Header('Cache-Control : max-age='.$age.', must-revalidate');
Header('Expires : '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified + $age));
echo file_get_contents($file);
}
Then you can use that in your image tag, like <img src="provider.php?q=foo.jpg" alt="Foo" />
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header('Content-Type: image/png');
imagepng($im);
//echo '<a href=\'imagepng('.$im.')\'> Download </a>';
echo "-----------------";
} else {
echo 'An error occurred.';
}
It shows the image but does not echo "==============".
You told the browser to expect an image, therefore it's only expecting an image. Everything sent will be considered part of the data for that image. And no, it won't convert text you send into part of the image.
This is because of your
header('Content-Type: image/png');
It prevents you from echoing something else than the picture on this page.
(Well it doens't, but your Browser thinks this still part of the picture)
If you want to echo picture and Text, you need a seperat file, e.g. like this
echo '<img source="./pic.php" alt="pic" height="20" width="20" />';
echo '______________';
wehere pic.php is the path to the file wich echos the picture.
There's a way of displaying both. As a disclaimer, I shall add not to use this in production, though. Not all browsers support this and it ain't purty:
header('Content-Type: text/html');
ob_start();
imagepng($im);
$data = base64_encode(ob_get_clean());
printf('<img src="data:image/png;base64,%s" />', $data);
print('---------');
You are generating an image (hence the header) so if you echo something after that, you are basically echoing "---" after the bytes of the image.
Your browser thinks your .php script is an image now and doesn't display the '---' as plain text and tries to add it to the image.