How to display frames that are separated from an animated GIF? - php

I'm trying to use GifFrameExractor. Full code for it is at the link if needed, but I dont think you will for the purposes of this question.
My code on my site is the following, using an animated gif of Homer Simpson...
$gifFilePath = 'images/homer.gif';
if (GifFrameExtractor::isAnimatedGif($gifFilePath)) { // check this is an animated GIF
$gfe = new GifFrameExtractor();
$gfe->extract($gifFilePath);
// Do something with extracted frames ...
foreach ($gfe->getFrames() as $frame) {
// The frame resource image var
$img = $frame['image'];
echo $img;
// The frame duration
$duration = $frame['duration'];
}
}
But my addition of "echo $img;" just outputs a bunch of resource IDs like "Resource id #8Resource id #10Resource id #12" and so on.
I googled and it says resource ID means you're trying to output a resource but you first need to extract info from the resource.
So how do I extract the actual img info so that I can display each frame from the animated gif?
I basically just want to display each of the frames of the animated gif down the page.
Any ideas?

Related

image from Database to a link in a tweet card (PHP)

I am trying to insert an image from database to twitter card (link) but the links appearing in the tweet without an image
example of the tweet card (link)
tried to make a default image for the tweet card and it work perfectly
but when i try to retrieve the name of the image from the database and put it in the <meta> it shows up as an empty image in twitter
tried also echoing $picture and the path shows up as its in the database : /image.png
<head>
<?php
//identify the user
$un = $_SESSION['active_user'];
//query to get that specific users information
$query = "SELECT * FROM users WHERE username='$un'";
//save the query in result variable
$result = $db->prepare($query);
//excecute the variable result
$result->execute();
//if the result has records in the database save the users info in the variables below
if ($row = $result->fetch())
{
//validation variables
$emptyFields = false;
$error = false;
$success = false;
$n = $row['name'];
$usn = $row['username'];
$picture=$row['picture'];
}
echo "<meta property='twitter:card' content='summary'>";
echo "<meta property='twitter:site' content=''>";
echo "<meta property='twitter:title' content=''>";
echo "<meta property='twitter:description' content=''>";
echo "<meta property='twitter:image' content='https://website.com/".$picture."'>";
echo "<meta name='twitter:creator' content='#abc'>";
echo "<meta name='twitter:text:title' content='website'>";
?>
what am i missing here ? is the way i am retrieving data ? please i would try any suggestion
i do not know how you saved the image to the location or to the database, but based on your question,
Read this and see if you missed something
twitter:image is a url to a unique image representing the content of the page. You should not use a generic image such as your website logo, author photo, or other image that spans multiple pages. Images for this Card support an aspect ratio of 1:1 with minimum dimensions of 144x144 or maximum of 4096x4096 pixels. Images must be less than 5MB in size. The image will be cropped to a square on all platforms. JPG, PNG, WEBP and GIF formats are supported. Only the first frame of an animated GIF will be used. SVG is not supported.
and also one more thing, it seems like you saved the entire or part of the image directory on the database, based on database : /image.png in your question. Note that this is not good practice since you might have to access the image from different pages of your website, and even though its not impossible, it's not worth it, just sve the image name only like image.png

Load Images and description into overlay (HTML/CSS/PHP)

I have a grid of thumbnails that when clicked on I want to load an overlay on the page showing the full size image and the contents of a text file as a caption.
Something like
<image src="thumbs/1001.jpg"> would just load the image, but I want to load the image in a light box sort of view and also load the contents to "desc/1001.txt" into a caption (not necessarily a <caption>) below the image.
I don't want it to reload the page if that can be avoided. If it helps, the images, thumbnails, and description all match the pattern above, or I also have a CSV file in the format of 1000,"Description of the picture" if that is simpler.
If you use something like https://lokeshdhakar.com/projects/lightbox2/#getting-started this should be fairly easy.
Your PHP code would look something like this:
<?php
$photos = [1000,1001];
foreach ($photos as $photo) {
$description = file_get_contents('desc/' . $photo . '.jpg');
echo '' . $photo . '';
}
?>

Image manipulation not updated in cache

On a page where users can rotate their uploaded picture, there is a button to rotate the picture. This is done using Intervention image, but the same result (and problem) has been achieved using PHP.
The problem is that the image rotation works, but the rotated image is not shown in the browser. This is probably due to cache, because when manually emptying the cache, the rotated picture is indeed shown in the browser.
public function rotate($picture_id) {
// get picture location
$path = Picture::where('id',$picture_id)->first()->location;
// Rotate the picture
$path_orig = storage_path('app/public/'.$path);
$img_orig = Image::make($path_orig)
->rotate(-90)
->save($path_orig);
// Rotate thumbnails etc
...
return Redirect::back();
}
SOLUTION
Update the filename with a random string.
public function rotate($picture_id) {
// get picture location
$path = Picture::where('id',$picture_id)->first()->location;
// Path to picture
$path_orig = storage_path('app/public/'.$path);
$path_s = public_path($path);
// New path
do {
$newKey = str_random(40);
$ext = File::extension($path_orig);
$dir = dirname($path_orig);
$path_L_new = $dir.'/'.$newKey.'.'.$ext;
$path_S_new = public_path('folder/'.$newKey.'.'.$ext);
}
while ( count(Picture::where('location',$path_L_new)->get()) > 0 );
// Rotate images
$img_L_new = Image::make($path_orig)
->rotate(-90)
->save($path_L_new);
$img_S_new = Image::make($path_s)
->rotate(-90)
->save($path_S_new);
// Delete old files
Storage::delete($path);
File::delete(public_path($path));
// Update location
$pic = Picture::where('id',$picture_id)->first()->update(array('location' => 'folder/'.$newKey.'.'.$ext));
// Show new picture
return Redirect::back();
}
You have to append a random or unique string at the of the image src:
<img src="/path/to/file.jpg{{ time() }}">
But be careful that don't append new random string to the end of the image src when the image is not being updated. If you do, the image won't be cached.
Proper way:
When an image updated, it's better to store a random string for this image and store it in database. And when displaying the images, append peer random string to it. Don't generate new random string on every call.

PHP Merge images (white color = transparency)

How to merge images if in image color white get transparency status white color?
I have this situation:
I need this result:
This is not a best result, since I didn't know better method to achieve this. Here is my result:
Here is the PHP:
<?php
/** Set source image location **/
$baseImageSource = 'http://q/stock-photos/tux.png';
$overlayImageSource = 'http://q/stock-photos/firefox-logo-small.jpg';
/** Overlay image configuration **/
// Set this value between 0 and 100. 10 will doing great
$fuzz = 10;
// Set position of overlay image, from top and left;
$overlayTop = 240;
$overlayLeft = 200;
/** Core program **/
// Create Imagick object for source image
$overlayImage = new Imagick( $overlayImageSource );
$finalImage = new Imagick( $baseImageSource );
// Remove overlay image background
$overlayImage->paintTransparentImage(
$overlayImage->getImagePixelColor( 0, 0 ),
0, round( $fuzz * 655.35 )
);
// Set image overlay format
$overlayImage->setImageFormat('png');
// Put overlay image to base image
$finalImage->compositeImage(
$overlayImage, Imagick::COMPOSITE_DEFAULT,
$overlayLeft,
$overlayTop
);
// Set output image format
$finalImage->setImageFormat('png');
// Prepare image and publish!
header('Content-type: image/png');
echo $finalImage;
Basically this is just a modification of this answer (to achieve image merger) and this answer (to achieve background removal). The method used to remove background is Imagick::paintTransparentImage(), with Imagick::getImagePixelColor() is used to detect background color. Then we just need to merge both image with Imagick::compositeImage().
But still, this result is far from perfect, especially if you compare it with image processing app like GIMP or Photoshop. But you should give it a try. Hope it helps :)
You need to use a program like Gimp, add a transparency layer, delete the white from the firefox image, and save as PNG.

display image thumbnail by reading path from table and retrieving the image from folder

I would be grateful if anybody could help me out with this.
I have a table which stores the filepath of a set of images, e.g col filepath stores values like: ./phpimages/image3.jpg. Note that my images are stored in folder 'phpimages'
Now i want to loop through all the rows in my table and display thumbnails of the images.
Here is my code:
/
*************************Display all records from images table***************/
//create an instance is Image
$objImage = new Image;
$result = $objImage -> listImage();
$num_rows = mysql_num_rows($result);
//echo $num_rows."records in database";
while($row = mysql_fetch_assoc($result)){
$imagepath="'".$row["filepath"]."'"; // note that if i put $imagepath= "dog.jpg", it displays the image just once!
//set mime type content
header('Content-Type: image/jpeg');
//create original image
$image = imagecreatefromjpeg($imagepath);
//get image dimension
$dim=getimagesize($imagepath);
//Set thumb dimension
$thumbw = 100;
$thumbh = 130;
//create empty image
$thumb_image=imagecreatetruecolor($thumbw, $thumbh);
//Resize original image and copy it to thumb image
imagecopyresampled($thumb_image, $image, 0, 0, 0, 0,
$thumbw, $thumbh, $dim[0], $dim[1]);
//display thumb image
imagejpeg($thumb_image);
}
?>
Please can anyone tell me where my error lies? Many thanks for any help provided
Why not just use <img src="">
You can output only one imagejpeg($thumb_image); using this method. If you want to display all thumbnails in a combined image, you should merge your images to one PHP/GD image, and then output that one.
If you would like to output thumbnail images, then I advise you to use the following tool:
http://phpthumb.sourceforge.net/
So when you iterate through your images, you should output an <img src="" /> for each thumbnail.

Categories