How to pass variable to PHP picture - php

Motivated by this post
https://security.stackexchange.com/questions/32852/risks-of-a-php-image-upload-form I want to display my images by
<?php $pathToPicture = "server/www/images/imagexyz1823014719102714123.png"; ?>
<img src="/resources/php/showImage.php" >
where showImage.php is simply given by
<?php
header('Content-Type: image/jpeg');
readfile($pathToPicture);
?>
But how can I pass the variable $pathToPicture to showImage.php? I do not want to hard-code $pathToPictue into showImage.php.

Pass the path of image as get parameter to showImage.php script like.
<?php $pathToPicture = "server/www/images/imagexyz1823014719102714123.png"; ?>
<img src="/resources/php/showImage.php?pathToPicture=<?php echo $pathToPicture;?>" >
Here you can get passed variable from $_GET array:
<?php
header('Content-Type: image/jpeg');
readfile($_GET['pathToPicture']);
?>
I preferably suggest use of base64_encode and base64_decode for pathToPicture for this purpose. Also not expose the whole path of your images location openly like this. Have a look at below improved code
<?php $pathToPicture = "imagexyz1823014719102714123.png"; ?>
<img src="/resources/php/showImage.php?pathToPicture=<?php echo base64_encode($pathToPicture);?>" >
<?php
$location = "server/www/images/";
$image = !empty($_GET['pathToPicture']) ? base64_decode($_GET['pathToPicture']) : 'default.jpg';
// In case the image requested doesn't exist.
if (!file_exists($location.$image)) {
$image = 'default.jpg';
}
header('Content-Type: '.exif_imagetype($location.$image));
readfile($location.$image);
?>

Related

Php display image from url into homepage

after no one answered at this question Php Rss feed use img in CDATA -> content:encoded i try to do something else solving this problem...
how can i load an image from a given url directly into my homepage?
<?php
$url = "...";
$image = file_get_contents("$url");
echo $image;
?>
*i don't want to save the image anywhere... just load the image from the url and show it in my own homepage.
Try this code,work fine on my machine.
<?php
$image = 'http://www.google.com/doodle4google/images/d4g_logo_global.jpg';
$imageData = base64_encode(file_get_contents($image));
echo '<img src="data:image/jpeg;base64,'.$imageData.'">';
?>
You are almost there. When you download the contents of the file you need to encode it to base64 if you do not plan to store it on server.
<?php
$url = '...';
$image = base64_encode(file_get_contents($url));
?>
Then you can display it:
<img src="data:image/x-icon;base64,<?= $image ?>">

PHP echo to display image HTML

I am trying to display an image on my webpage using a PHP script to determine which image is displayed.
The image link is as follows:
......
My PHP script is thus:
<?php
$result = $_GET['image'];
echo '<img src="images/gallery/'.$result.'.jpg">';
?>
So what I am trying to achieve in terms of HTML is:
<img src="images/gallery/image01.jpg">
The result I am getting is '"; ?>' displayed on the page.
Any help would be much appreciated!
You have to change your code like this
<?php
$result = $_GET['image'];
?>
<img src="images/gallery/<?php echo $result; ?>.jpg">
<?php
$result = filter_input ( INPUT_GET , 'image' );
if (isset($result) && !empty($result)) {
echo '<img src="images/gallery/'.$result.'.jpg">';
}
?>
You used echo wrong, here is how you should use it.
<?php
$result = $_GET['image'];
?>
<img src="images/gallery/<?php echo $result ?>.jpg">
I would change the gallery.php to this:
<?php $result = $_GET['image']; ?>
<img src="images/gallery/<?php echo $result; ?>.jpg">
That would simply it a little bit. You should echo out the result to see what you are getting when the variable is passed to the gallery page.
echo"<img src='{$image}'>";
$image = uploads/myImage.jpg
I think this is the simplest code. To use a php variable while echoing out html, use curly {} brackets to insert any php variable. For instance, a file upload...
<?php
if(isset($_POST['submit'])){
$filename=$_FILES['file']['name'];
$temp_dir=$_FILES['file']['tmp_name'];
$image = "img/".$filename;
}
?>
<?php if($row2['pack1']==1){ echo "<img src=".BASE_URL."images/1seo.png"; } ?>

Alternative image (via IF function) isn't working

i am trying to get a Minecraft player's skin via PHP, and it all works, until the user enteres an invalid username, techniclly, if that happenes the script should replace the not found image with an alternative image, but for some reason it doesn't.
full script:
<?php
//initial settings
header("Content-type: image/png");
//declere values
$name = $_GET['n'];
//get the image from Minecraft main servers
$src = imagecreatefrompng("http://s3.amazonaws.com/MinecraftSkins/{$name}.png");
//if not found, use an alternative image
if(!$src){
$src = imagecreatefrompng("http://s3.amazonaws.com/MinecraftSkins/char.png");
}
//display the skin
imagepng($src);
?>
Any help will be appriciated, thank you.
try
<?php
//initial settings
header("Content-type: image/png");
//declere values
$name = $_GET['n'];
//get the image from Minecraft main servers
if(file_exists("http://s3.amazonaws.com/MinecraftSkins/{$name}.png")) {
$src = imagecreatefrompng("http://s3.amazonaws.com/MinecraftSkins/{$name}.png");
//if not found, use an alternative image
else {
$src = imagecreatefrompng("http://s3.amazonaws.com/MinecraftSkins/char.png");
}
//display the skin
imagepng($src);
?>

Secure images failing - Codeigniter

My script pretty much works, however when I run a for loop it fails, in the view:
<?php foreach($pics as $index=>$pic){?>
<td class='Pictures'>
<center>
<?php
$piece = explode('/',$pic["ThumbImg"]);
$string = $piece[5];
?>
<img src='<?php echo base_url(); ?>index.php/Controller/getImage/<?php echo $string; ?>/1' width="100px">
</a>
Controller class - Get image function:
//function to protect images from being accessed directly by obfuscating the URL
function getImage($img_id,$type){
if($this->members->logged_in()){
//code to authenticate user goes here then...
//code to decide which type of file it is/thumb/full size image
if($type==1)
{
$url = $this->data['base_url'].'system/application/images/pic/thumbs/';
}else if($type==0){
$url = $this->data['base_url'].'system/application/images/pic/';
}
$filepath = $url.$img_id;
//send image to web browser.
header("Content-type: image/jpeg");
//get path
$img_handle = imagecreatefromjpeg($filepath) or die("");
//create and send
ImageJpeg($img_handle);
}
I suspect that this could be due to the headers being resent, if so how can I resolve this.
Cheers
Fixed the problem - used "Hotlinking"
You will basically only need to set the header information header("Content-type: image/jpeg") if you're loading the image from the database. But since you're loading it from a folder you might want to skip the php file and immediately fetch the image from the folder:
<img src='<?php echo base_url(); ?>img_folder/<?php echo $string; ?>/1'
width="100px">

Error when get url name of images?

I have a code download image from link http://www.bitrepository.com/download-image.html
When start is link format: <img src='test[1].jpg'>
But when download this link is link become <img src='test3%5B1%5D.jpg'>
How to fix it?
code here
<?php
include_once 'class.get.image.php';
// initialize the class
$image = new GetImage;
$image->source = 'http://test.com/test[1].jpg';
$image->save_to = 'images/'; // with trailing slash at the end
$get = $image->download('gd'); // using GD
if($get)
{
echo 'The image has been saved.';
}
?>
Try this.
On this line
$image->source = 'http://test.com/test[1].jpg';
Changed to
$image->source = htmlspecialchars_decode('http://test.com/test[1].jpg');
Look up urldecode in php to change the encoded values back to brackets

Categories