PHP Image from URL - php

I have the following url which returns an image in the body, How would I assign that image to a variable so that I could save it or use it within my code?
https://media.licdn.com/dms/image/C4E03AQGOM3p0fHNkwQ/profile-displayphoto-shrink_100_100/0?e=1556755200&v=beta&t=qeQFKYXpev2ZW3hmP1ODDPd3DYPWvl-GaUnPSZG-aQA
The following code returns noting:
<?php
$url = "https://media.licdn.com/dms/image/C4E03AQGOM3p0fHNkwQ/profile-displayphoto-shrink_100_100/0?e=1556755200&v=beta&t=qeQFKYXpev2ZW3hmP1ODDPd3DYPWvl-GaUnPSZG-aQA";
$image = file_get_contents($url);
echo $image;
?>

If you simply want to view an image from a URL, just put that url in the src attribute of an img element like this :
echo '<img src="' . $url . '"/>';
otherwise, if you have to view it from the fetched data of that url, then your question have been answered here:
php: recreate and display an image from binary data (specifically #Krab's answer)

Related

php image database blob

need your help. I am able to upload image to database on a blob field. Now the question is I am not able to display it back, it just puts out a box with out the real image, what is that I am missing.
Here is the code. images is the field that is declared as blob;
$statement->bind_result($notes, $images, $image_type);
$statement->fetch();
echo $images;
echo '<img rc="data:image/jpeg;base64,'.base64_encode($images).'"/>';
you missed src from img tag. use src in place of rc
echo '<img src="data:image/jpeg;base64,'.base64_encode($images).'"/>';
you missing 'r' in your attribute src <img> tag :
echo '<img src="data:image/jpeg;base64,'.base64_encode($images).'"/>';

Why is a specific variable causing html to print out

I am trying to display some images in my wordpress site. I am trying to make the background div and image via style=""
So when I do this:
<?php
if(has_post_thumbnail($property->ID)){
$image_url = get_the_post_thumbnail($property->ID,array(300,220),array('class' => "post_thumbnail"));
}else{
$image = $image_url = '';
}
?>
<div class="property_photo" style="background-image:url('<?php echo $image_url;?>') no-repeat;">
</div>
The photo is not displaying anymore and it's printing ') no-repeat;"> where the image should display. When I use a variable without an "_" in the name, it doesn't print anything. So to avoid that I tried $image = $image_url = ''; to get around that.
Is this is a simple syntax problem or is there something in the php that is causes this? It doesn't seem like syntax because when I use other variable it does not do this.
Your get_the_post_thumbnail() function is returning HTML, not a URL. You need to either change the function to only return a URL (or use a different function which does that), or change your HTML to put the image inside the div instead of setting it as a background image.

PHP images from function

I am a php newbie so please go easy.
I created queries for imgFld and imageFldName.
I am trying to find why my images from my db are not being displayed.
I have the below code:
image_show(stripslashes($row['imgFld']),stripslashes($row['imageFldName']));
echo ' '.$records_num;
function image_show($name_image, $alt_tag) {
if (file_exists("mywebsite.co.uk/images/'$name_image'")) {
$img = getimagesize('mywebsite.co.uk/images/'.$name_image);
echo '<img src="mywebsite.co.uk/images/'.$name_image.'" alt = '.$alt_tag.' border=0 align="bottom"';
echo 'width = '. $img[0] .' height = ' .$img[1] . ' />';
} else {
echo 'Add an image here';
}
}
Im getting the image names from a column inside my db and each column has an 'image.jpg', connecting it with the img src script from HTML so that I can display the images from the db.
However no images are being displayed and I cant find the error. Doesnt seem like anything is wrong.
When I echo $name_image nothing is produced.
In that case it means that there is nothing populating the $name_image variable.
Assuming that website directory is a local one, the cause of this is most likely your arguments when you call the image_show function. They do not match the order you have specified.
The first argument should be the name and the second the alt text, as defined:
function image_show($name_image, $alt_tag)
However you are passing the id to $name_image and the name as $alt_tag.
That should be it.
The problem is file_exists is only for local files.
For example:
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/path/to/files/image.jpg')) {
...
}

Wrap a link around image in slideshow using wordpress magic fields

I have a slideshow set up with Magic fields like the code below, but
now I need each image to have a seperate link. How can I set this up?
I just can't think how I can add this to the code below, I appreciate
any help anyone can offer me.
<div id="slider">
<?php
$images = getFieldOrder('slideshow_slide');
if(is_array($images)){
foreach($images as $image){
echo get_image('slideshow_slide',1,$image);
}
}
?>
</div>
Hooray MagicFields! <3
There are two ways to get an image in MagicFields.
Method 1 will return a full image tag:
echo get_image('slideshow_slide');
Method 2 just returns the url of the image:
echo get_image('slideshow_slide',1,1,0);
In order to generate a link to your full-size image, you'll need to construct an anchor tag using the second method. Maybe something like this:
$image_path = get_image('slideshow_slide',1,1,0);
echo 'Insert link text or thumbnail here';
You might need to modify the above to work with your foreach loop, but that's the basic idea.
Update:
Here's what you need to do. Create another duplicateable text field, called image_url. This field will hold the link for your image. Each image will need a corresponding url. This loop should do what you want:
if(is_array($images)){
foreach($images as $image){
$image_url = get('image_url',1,$image);
echo "<a href='" . $image_url ."'>" . get_image('slideshow_slide',1,$image) . "</a>";
}
}

PHP: Image retrieval from MySQL Blob directly into <img> tag

I've stored my Images into (Medium) BLOB fields and want to retrieve them embedded within my PHP-generated web pages.
When I test retrieving the stored images using
header('Content-type: ' . $image['mime_type']);
echo $image['file_data'];
everything looks just fine.
However, I have not yet found a way to retrieve the image(s) cleanly into the middle of my documents. For example, using
$image = $row['file_data'];
echo '<img src="data:image/jpeg;base64,'.$image['file_data'].'" alt="photo"><br>';
...or...
$im = imageCreateFromString($image);
I just wind up with a bunch of hexadecimal garbage on screen.
I intitially stored the Images using:
ob_start();
imagejpeg($resizedImage, null, 100);
$content = ob_get_contents();
ob_end_clean();
$sql = sprintf(
"insert into images (filename, mime_type, file_size, file_data, event_id)
values ('%s', '%s', %d, '%s',%d)",
mysql_real_escape_string($fileName),
mysql_real_escape_string($mimeType),
$imageSize,
mysql_real_escape_string($content),
$eventID
);
$result = $cn->query($sql);
Does anyone PLEASE have a working code snippet to successfully display the stored .jpg mid-file in the PHP output?
echo '<img src="data:image/jpeg;base64,'.base64_encode($image['file_data']).'" alt="photo"><br>';
However, remember that old IE versions do not support this kind of inline images! Besides that, the browser cannot cache such an image except together with its containing HTML page.
You should create some sort of "image server". You're already close to that.
For example, create something like image.php that will get a image name and will generate it on the fly.
So, for example, say you want to get somePic.jpg image. You can get it through:
image.php?name=somePic.jpg
<?php
header('Content-type: ' . $image['mime_type']);
echo $image['file_data'];
?>
Your tag:
<img src='image.php?name=somePic.jpg' />
Or more general:
echo "<img src='image.php?name={$image['filename']}' />"
Why not just call your test page image.php, then have it called from the browser on the rendered page:
<img src="image.php?imageid=123" alt="photo" />

Categories