I currently have a imagecreatefrompng function and it works, but when I use an IF statement on it, then it doesn't work and shows it cannot load image... here is what I have:
$design = $_GET["design"];
if ($design == "DESIGN_1") { $image = imagecreatefrompng('designs/hill.png'); }
if ($design == "DESIGN_2") { $image = imagecreatefrompng('designs/hill2.png'); }
In the header I have:
http://www.website.com/create.png?design=DESIGN_1
it displays the HILL.PNG
But when I have this following in header:
http://www.website.com/create.png?design=DESIGN_2
it doesn't display HILL2.PNG but shows the image not found symbol.
PS. Both images are in the designs folder.
Can you view the second .png in a browser if you navigate to it? Could it be a malformed image file? Since the code appears to be fine, and it's actually trying to display an image... I have to think that there's a problem with the image itself.
The PHP code is OK.
Probably there's an error with the image "designs/hill2.png"
Check it with
<img src="http://www.website.com/designs/hill2.png"/>
Related
I'm displaying an image where the URL is kept in the database, now i want to display it completely black if a condition isn't met
The URL
$url = '/images/'.$row['sprite'].'.png';
its then displayed in a normal image tag
What i want is if $row['normal'] == 0 then black the image, making it a silhouette, otherwise display the normal image
After some searching I've found about imagefilter but am not sure how to apply it, as the examples i've found don't show how to apply it when there is other content on the page
Or would it be better to make the silhouettes in photoshop, given that there is over 800 of them, though only a maximum of two on the page
Firstly you need to load GD Image Library to your server.
Define your image path and create an image object by using imagecreatefrompng if your image types are different choose correct one.
$image_path = $_SERVER['DOCUMENT_ROOT']."/assets/img/horse1.png";
$image_obj = imagecreatefrompng($image_path);
Now, we need to apply a filter, if your conditions provided. Using the imagefilter function to apply any filter to your image. In this example IMG_FILTER_GRAYSCALE is fair enough or you can change it by using the manual of function.
if($row['normal'] == 0) {
$op_result = imagefilter($image_obj,IMG_FILTER_GRAYSCALE);
}
Finally, we need save the image to server using by imagepng function.
imagepng($image_obj,$_SERVER['DOCUMENT_ROOT']."/assets/img/horse1_black.png");
Check the full code below beacuse I strongly suggest that, you shouldn't create black image for every single user. If your image is already exist in your server just show it without any creation.
$image_path = $_SERVER['DOCUMENT_ROOT']."/assets/img/horse.png";
$black_image_path = $_SERVER['DOCUMENT_ROOT']."/assets/img/horse_black.png";
if($row['normal'] == 0) {
if(file_exists($black_image_path)){
return $black_image_path; //if your black image is already exist just return and use it.
}
else {
$image_obj = imagecreatefrompng($image_path); //create a image object from a path
$op_result = imagefilter($image_obj,IMG_FILTER_GRAYSCALE); //applying grayscale filter to your image object.
if($op_result) {
imagepng($image_obj,$black_image_path); //save the image to defined path.
return $black_image_path;
}
else {
return "Error Occured.";
}
}
}
I am trying to get a picture from the database and echo this on my website, and everything works like I want to, but now I got this tiny problem where if the image has no name yet, or it errors (like a 404 error), that I get a ugly tiny broken image icon.
Can I get it to work that I get a default image when I get the error? I tried this, but it only worked for where there was no name set yet.
if(!empty($filename)){ echo $filename;} else{ echo "pf.png"; }
Insert this in the image tag:
onerror="this.src='[img src]'"
Since you say it is working with "pf.png", I assume that you are at least using the filename correctly. So if you get that broken image, it means the resource could not be loaded, because it does not exist or cannot be retrieved.
So you also have to check that the image exists before trying to use it anyhow :
if(!empty($filename) && file_exists($filename) {
echo $filename;
} else {
echo "pf.png";
}
To do this i php, you will have to check if the file exists. You wont be able to do this if the link to the image is over "http:/www.*"
Clément Malet code above works fine.
You can read more about file_exitsts function here:
http://php.net/manual/en/function.file-exists.php
If the link to image is over "http:/www.*" you can use jquery.
Put this code before </body> Remember to include jquery
$(document).ready(function(){
$("img").error(function(){
$(this).attr("src", "http://google.dk/default.php");
})
.each(function() {
$(this).attr("src",$(this).attr("src"));
});
})
I need to send a GET request to my page pic.php, and I want to get a real picture in return.
For now I implemented this idea like this:
<?php
if ((isset($_GET['pic']))&&(isset($_GET['key']))){
$pic = $_GET['pic'];
$pic=stripslashes($pic);
header('Location: /content/'.$pic);
}
?>
But it's not really what I want - it redirects to image directly. What I want is to keep the same URL, but get a needed file depending on what values were submitted.
What is the best way to do that?
thx.
This example code snippet should do what you ask. I've also included code to only strip slashes if magic quotes is enabled on the server. This will make your code more portable, and compatible with future versions of PHP. I also added use of getimagesize() to detect the MIME type so that you output the proper headers for the image, and do not have to assume it is of a specific type.
<?php
if(isset($_GET['pic']))
{
//Only strip slashes if magic quotes is enabled.
$pic = (get_magic_quotes_gpc()) ? stripslashes($_GET['pic']) : $_GET['pic'];
//Change this to the correct path for your file on the server.
$pic = '/your/path/to/real/image/location/'.$pic;
//This will get info about the image, including the mime type.
//The function is called getimagesize(), which is misleading
//because it does much more than that.
$size = getimagesize($pic);
//Now that you know the mime type, include it in the header.
header('Content-type: '.$size['mime']);
//Read the image and send it directly to the output.
readfile($pic);
}
?>
I can see you doing this in two ways:
1) Return the URL to the image, and print out an image tag:
print '<img src=' . $img_url . ' />';
2) Alternatively, you could just pull the data for the image, and display it. For instance, set the header appropriately, and then just print the image data.
header("content-type: image/png");
print $img_data;
This assumes that you have the image data stored in a string $img_data. This method will also prevent you from displaying other things on the page. You can only display the image.
You can load the image, send the image headers, and display the image as such:
header('Content-Type: image/jpeg');
readfile('/path/to/content/pic.jpg');
Obviously the headers would depend on the filetype, but that's easy to make dynamic.
Not sure if I understand what you're after, but guessing that you want to load the picture in an img tag?
If I'm right you just do:
<img src=http://www.domain.com/pic.php?"<?php echo image here ?>" />
Basically you just make the source of the image the webpage you get directed to where the image is.
<?php
if(isset($_GET['img']) && is_numeric($_GET['img'])){
$img = $_GET['img'];
$imgarray = array (
'1' => 'http://www.path/to/image1.png',
'2' => 'http://www.path/to/image2.png',
'3' => 'http://www.path/to/image3.png'
);
$src = $imgarray[$img];
header('Content-type: image/png');
echo file_get_contents($src);
}
else
{
header('Content-type: image/png');
echo 'Image could not be loaded';
}
?>
Hello again stackoverflow!
Im having multiple problems.
1: When the $_GET['img'] is set and its numeric, the image will be displayed right, but i want to add text in the upper-right corner of the image... How can i do that? I've looked through multiple GD tutorials and examples but i can't find my answer.
2: When $_GET['img'] isn't set i want to display the text: Image could not be loaded. How cna i do that? Because this doesn't seem to work...
Greetings
What you'll have to do is use GD. Load up the requested image into PHP with imagecreatefrompng(), since you have listed pngs in your array, you'd have to use imagecreatefromjpeg() or whatever depending on their format. Then use one of the text writers like imagestring() to write the text to the location in the image resource returned by imagecreatefrompng(), then return the image resource to the browser.
Can also use one of the functions that uses an external font, like imagettftext(), but would need to have the appropriate font to use on the server.
For the error, if you want it to be an image, you'll need to use imagecreatetruecolor() to make a new image, then use imagecolorallocate() to assign a color palette to it, then use imagestring() to write the error message to the image and return it. Of course, probably be easier just to make an error image in GIMP or something and return it, rather than going through the trouble of generating a new error image each time.
Just remove the line that says header('Content-type: image/png'); in your else{} block
That will do the trick. At the moment your are telling the user's browser to treat that text as an image, of course that can't work. If you want an image with the text "Image could not be loaded", it's more complicated than that...
I am working with a script for resizing images. I seem to be getting an error:
Error: no image was specified
Probably because of this code in the script(image.php):
if (!isset($_GET['image']))
{
header('HTTP/1.1 400 Bad Request');
echo 'Error: no image was specified';
exit();
}
Here is what I'm doing(profile.php):
$your_image = $row['Image'];
$path_to_image = $row['PortraitPath'];
$width = 100;
$height = 100;
echo "<img src=\'/image.php/{$your_image}?width={$width}&height={$height}&cropratio=1:1&image={$path_to_img}\' alt=\'Alt text goes here.\' />";
Therefore, I am reading $your_image and $path_to_image from a MySQL table, and then putting it in the img source. As mentioned above, obviously, image is not set, that is why I am getting that first error. What I don't get is, how will the image actually even be set with my img src code? Aren't I simply displaying the actual image? Then how will image even be set if a picture is simply being displayed? Thank you.
If you want to source a php file instead an image, you need to tell your php file that the output will be an image.
You can do this using the php header() function, like this:
header('Content-type: image/jpeg');
Here is some reference: php header function
About the address you are point to, isn't a bit weird? You have a slash right after the .php, which suggest that you are trying to access some folder... Did you tested this url to see if a real image are being outputted on the screen?
Hope this can help you =)
The URl for the image contains ?foo=bar&this=that&image=path. These variables will be passed to the image.php script in the $_GET array.
As a word of warning, in your profile.php's code I saw this fragment:
image={$path_to_img}
Depending on how you deal with the value of $_GET['image'] this may result in a RFI vulnerability. The user could forge a GET request to image.php with their own "image" path.
A couple things that I noticed, I'm not sure how much of the code you modified before posting it here...
1a) Don't escape the single quotes if you are using double quotes to encompass it.
OR
1b) Change the escaped single quotes to escaped double quotes.
2) In the URL you are using $path_to_img but the variable you have defined is $path_to_image. Make them consistent.