I've been trying to display a gif image using PHP and have not found a solution to my problem with the research I've done. I know I can display image in HTML/CSS, but I need to use php in this case.
<html>
<?php
$img = imagecreatefromgif("http://www.mysite.com/images/timer.gif");
?>
<img src="<?= $img ?>" alt="timer" />
</html>
That code resides in a php doc on my server. I can tell the code is working because an icon of a torn image displays on my site, and when I attempt to save the torn icon image to desktop, the automatic file name appears as "Resource id.html"?
I read somewhere that creating gifs with Photoshop CS5 (as I did) uses a different frame separator sequence, \x00\x21, instead of the official standard \x00\x2C. The guy then said he uses pattern "#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s" to bypass that issue but I have no idea how/where to implement that or if that is even my issue (tried a different gif from internet and had same display problem). Thoughts? Thank you.
Imagecreatefromgif() returns an image resource, which you can't use in this way.
Have your PHP script output the image data, with the correct headers (see example here http://php.net/manual/en/function.imagecreatefromgif.php).
Then you can call your script just like you would any other image:
<img src="http://www.mysite.com/images/generate_image.php" alt="timer" />
Related
I'm building a little site at the moment and users can upload their own profile picture but obviously some users pictures will be png and some jpeg, does anyone know anyway to be able to accept both when using the images as a background image in css. Here is my HTML:
<div class='avatar' style='background-image: url('users/avatars/$username.jpg')'></div>
Inside the users/avatars/ folder is every users profile picture named like so: "username.jpg" or "username.png".
Here is an example of my issue: http://cl.ly/image/3W311S2v0v0t
The first post loads the users profile image because it is a jpeg but the second post hasn't loaded the profile image as it is a png.
Thanks in advance.
You can do it in CSS like this:
background-image: url('users/avatars/$username.jpg'), url('users/avatars/$username.png');
If you absolutely must keep all the logic on the client side, in Javascript, you can catch the error if the image fails to load and attempt to load the other version.
For example:
<div class='avatar'>
<img src="users/avatars/$username.jpg" onerror='this.src="users/avatars/$username.png";' />
</div>
So if the JPG is not found and returns an error, the image will then load the PNG version.
If you are using PHP you can check the file extension and serve up different HTML. Here's how to read the file extension:
How to get the file extension in PHP?
i'm using shadowbox for my website to open the big images with clicking thumbnails as you know. My Problem is, i'm fetching the users' facebook profile photo like :
$large = "https://graph.facebook.com/{$id2}/picture?type=large";
$small = "https://graph.facebook.com/{$id2}/picture?type=square";
And it's working perfect, but in shadowbox i have problem with large image..
I'm calling this in shadowbox like :
<a href="<?php echo $large; ?>" rel="shadowbox">
<img style="max-width:50px; max-height:50p;" src="<?php echo $small; ?>" />
</a>
As you can imagine, small image is showing perfect, but when i click on the small image which has href, it fails to show the large image.
I've tried to change large image variable to this :
$large = "https://graph.facebook.com/{$id2}/picture?type=large&redirect=false";
but also it has failed to show the large image..
Hope you can help, thank you
I haven't worked with Shadowbox before, so I don't know how it works behind the scenes. It may be that it can't deal with a url that returns another url for the large image.
Try making the API call with php and then passing this result to shadowbox.
$large = file_get_contents("https://graph.facebook.com/{$id2}/picture?type=large&redirect=false");
Of course, using file_get_contents() like this is a quick and dirty method. If it works, before you roll this out to a production site, you'll want to use cURL or better yet the Facebook PHP SDK to do this.
Assuming you're using shadowbox to show multiple users' photos on a page, you'll probably end up bumping into the API limits at some point. To prevent this, redo your API calls to grab multiple photos in one shot:
$large_photos = $fb->api('/picture?type=large&redirect=false&ids=' .
implode($ids_array, ','));
https://graph.facebook.com/{$id2}/picture?type=large&redirect=false basically returns you a text content the URL of the image. That's why it's not showing.
Eg.
https://graph.facebook.com/yungsenriady.budiman.3/picture?type=large&redirect=false
returns
"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/157348_100001167523294_1569184886_n.jpg"
Try just use
https://graph.facebook.com/{$id2}/picture?type=large
I have the following script
<div class="left">
<img src="image1.jpg" height="186" width="160" />
</div>
<div class="center">
<img src="image2.jpg" height="186" width="160" />
</div>
<div class="right">
<img src="image3.jpg" height="186" width="160" />
</div>
which generates the following :
I have this simple form to upload an image:
<form action="upload/image" method="post">
<input type="file" name="upload_image" />
<input type="submit" />
</form>
The reason to upload an image is to see how the mug(above) would look like with the image.
Now after uploading the image the mugs would look like following
Figure-2
Now if you kindly take a closer look at the above image you will see the uploaded image is sticked with the mug in such a way that as if it was designed with Photoshop but actually it was not .
Now my question is would you please kindly tell me how to paste the uploaded image on to all the three images of the coffee mug and make them look like the figure-2.
Thanks in Advance :)
P.S
I got the idea from this site-> http://www.zazzle.com/cr/design/pt-mug
I am trying to achieve the same thing the website has(I have been trying for so long but yet no result... failing to put the the uploaded image on the images of the mug ).
Here's the download link for the image I used here http://i43.tinypic.com/d66h4.png
Yes, you can do this.
If you examine the given url from Zazzle.com, after uploading the image, it is calling a program on their server - "designall.dll". It is doing all the magic.
As our other friends mentioned, it is not possible to achieve that effect simply with HTML, CSS and JavaScript (without using Canvas).
But using some server side program you can achieve this without any issues.
As I program on LAMP, I am giving a solution to implement the same with PHP and ImageMagick.
User uploads an image.
Image will be sent to a back-end PHP file.
PHP will distort the image as per our requirements.
Creates three copies and store in a temp folder with some unique ID.
As a response to that we will get some success code.
Once we get a success code, we will load the images from temp folder.
As the user clicks on any type of image, we will update the image as required.
And regarding the actual distortion code, you can use ImageMagick plugin.
More information about ImageMagick plugin can be found at :
ImageMagick Website
More information about the ways to distort (actually wrapping) an image using ImageMagick can be found at : Wrapping and Distrotion. These links contain good number of examples with preview too.
For our example, we need to create some static code which will distort an image every time in same shape.
Please let me know if I am missing something.
Friends, please help "black_belt" with better solutions if any.
Thanks.
I am using this script which is one of the examples provided by jpgraph itself. When I put this on a web-page (blank) by itself, it's drawing the graph. But when I embed the code in already existing web-page (with some content), it ain't drawing a graph.
GD is already enabled according to phpinfo(). Iam using jpgraph 3.5.0b1.
The problem is that you are mixing HTML/text output with image output.
Any time you have a PHP script generate graphical content you have to handle the output differently than normal HTML or text.
There are a few routes, I'll cover them briefly here.
Save the output to a file and use that filename in your HTML
//replace this line:
// Display the graph
//$graph->Stroke();
// with these lines:
// Default is PNG so use ".png" as suffix
$fileName = "/tmp/imagefile.png";
$graph->img->Stream($fileName);
.. then use $filename in an image tag, like this (for example):
print '<img src="'.$filename.'" />';
Create a standalone PHP script that will output the graphic
You can use the example script as-is, alone in a file called graph_render_script.php. Then, in your HTML, you use that script as a source:
<img src="graph_render_script.php" />
Output base-64 encoded data
Another route is to use base-64 encoded image data. This is relatively simple to do:
print '<img src="data:image/png;base64,'.base64_encode($graph->Stroke()).'" />';
As always, the documentation should be your guide!
Documentation
http://jpgraph.net/download/manuals/chunkhtml/ch05s05.html
base64_encode - http://php.net/manual/en/function.base64-encode.php
This worked for me:
putting the php code that generates the image in a file...Then on my html page I do this:
<img src="graph.php" >
embedding the graph inline is indeed possible. You'll have to use output buffering to capture the image data, then base64 encode that data, then use that base64-encoded string as the source in an <img>.
Try something like this:
$img = $graph->Stroke(_IMG_HANDLER);
ob_start();
imagepng($img);
$imageData = ob_get_contents();
ob_end_clean();
?><html>
<head>
<title>JpGraph Inline Image Example</title>
</head>
<body>
<h1>JpGraph Inline Image Example</h1>
<img src="data:image/png;base64,<?php echo(base64_encode($imageData)); ?>" />
</body>
</html>
ceejayoz made an excellent point in that this method is almost never what you want. I do not recommend embedding the image data like this unless you have a good reason to do so and understand the downsides, i.e. older browsers lack support for it, and the page size is dramatically increased (not only from the image data but the fact the image data is base64 encoded, which balloons the length). I've used this method in the field myself on a project last year, but it was only because the client refused to make a second request for the image.
But when I embed the code in already existing web-page (with some content), it ain't drawing a graph.
You can't do that - you can't output an image as raw binary data within a page.
You need to put the code that generates the graph in a separate file, and use an image tag.
<img src="path/to/jpgraph/chart.php" />
The graph needs to be on its own page, you can't embed it. It outputs a raw JPG and you need to have no other content sent and have the proper headers to tell the browser it's a JPG. To embed the graph you'd make a different page called stats.php for example, and on that page you'd make an image tag pointing to the stand alone graph.
<img src=graph.php>
I've had this problem many times, I've noticed it happens when you have require() or include() in your Chart's script and those scripts have Data Base connections or special configurations.
I've solved this problem separating the data retrieving and the Chart drawing, passing parameters to the script or using SESSIONS to get the values.
Example of Embed image Chart in your PHP or HTML file:
<img src="linear_graph_customer.php?values=1,2,3,4|1,2,3,4|1,2,3,4&title=CHART&width=500&height=300" width="500" height="300" class="img" />
Regards.
Ok people, despite the best-known-practices, today I decided to do this:
<img src='<? include("dir/dir/img.png"); ?>'>
With 6 diferent .png images.
Sadly, only 2 of the 6 were nicely visible on the browser.
Why only 2 of the 6 images were shown? Maybe there were data losses bits on the way?
Thank you for your time :]
It does not work because src attribute of an <img> tag is not supposed to contain the raw data of an image; rather, it is supposed to contain a URI that points to the image data.
By using data: URIs, you can embed the image directly in your (X)HTML document. Note that this will not work in many browsers such as older versions of Internet Explorer. As well, there are limits, such as the 32KB limit IE8 places on data: URIs.
Using PHP, here's what your code would look like:
<img src='data:image/png;base64,<?php echo base64_encode(file_get_contents("dir/dir/img.png")); ?>'>
Don't forget to change the image/png part of the URL if the type of image that you are using changes. For example, if you use a GIF image, change it to image/gif.
That was not supposed to work at all.
For a standard way to do that (including images inline in the HTML document instead of pointing to their URL), see the data URI scheme.
include() tells PHP to parse that file. If, by any chance, it contains <?, you’ll be in real trouble. Instead, use readfile().
Additionally, Artefacto’s answer has to be considered as well.
< img src='< ?php echo 'data:image/png;base64,' . base64_encode(file_get_contents('dir/dir/img.png')) ; ?> ' >