PHP, getting Thumbnail for an image in Elfinder - php

I use Elfinder 2.1 and i'm looking for a possibility to get the right thumbnail path of an image with PHP.
By default a .tmb folder exist in every folder and contains the resized thumbnails with an (i think) md5 hashed filename.
How can i retrieve the correct thumbnail for a specific image in PHP?
The goal is to show only the thumbnails in another PHP Script and with a click the original imgage shows up.

Thanks, was useful. I added one thing.
protected function tmbname($stat) {
$ext = 'png';
if($stat['mime'] == 'image/jpeg'){$ext = 'jpg';}
if($stat['mime'] == 'image/gif'){$ext = 'gif';}
return current(explode('.', $stat['name'])).'.'.$ext;
//$stat['hash'].$stat['ts'].'.png';
}
Works well and deletes too.

Take a look at this issue, it may point you in the right direction:
https://github.com/Studio-42/elFinder/issues/671

I answered that question just a few minutes ago on the above link. In short:
search for the function tmbname($stat) in the class elFinderVolumeDriver.class.php
replace the return value with this: current(explode('.', $stat['name'])).'.png';
This way the created thumbnails will have the same name as the original image (of course with .png extension). If that will arise security questions/problems, I don't know. Hope it does help.

Related

How to show the last uploaded image when refresh page in PHP

I have a application which is generating images with file_get_contents and file_put_contents method from a dynamic image source. After creating the image the image is uploaded to a directory on my server. The problem I face is every time the image is generated and I refresh the page I see the old image appear. It shows the new image when I clear the cache.
How could I solve the issue?
<?php
$imagename = "img".$id.".png";
$host = $_SERVER['DOCUMENT_ROOT'];
$path = $host.'/url/path/img/'.$imagename;
if(file_exists($path)) {
//echo 'File already exists in that directory';
unlink($path);
$filehandler = file_get_contents($imgurl);
file_put_contents($path, $filehandler);
}
else {
$filehandler = file_get_contents($imgurl);
file_put_contents($path, $filehandler);
}
It would always be better to use version as query Param to all images, js and css files. Using timestamp will not be as good since it will not be shown from cache if image is not updated. So in order to avoid additional overhead use version of images
That is,
Initially
<img src="image.png?v1" />
Till now next updation occurs this should be the URL
Once it is updated, it should change to
<img src="image.png?v2" />
As the other answers already stated, simplest solution is to add a parameter to the image URL. To still leverage browser caching when the image is unchanged, you should not use a random value.
My recommendation is to use the modification time of the image file, e.g.
<img src="image.png?<?php filemtime('image.png'); ?>" />
This has the advantage, that you don't need to keep track of versions to increment a number or similar.
This can be solved by adding random parameters to the image src, as shown here. For example, turn this:
<img src="image.png" />
Into this:
<img src="image.jpg?1222259157.415">
In the second src, the numbers after the question mark are the current timestamp. This is demonstrated in this answer, which sets the timestamp in JavaScript and appends it to the src using Date.now().

Need to create dynamic thumbnail grid.

I am new in web development, i need to create a website for portfolio purposes in which i need to show thumbnail and a little description of my project underneath it and all content should be dynamically displayed. I have basic knowledege of PHP, wordpress, javascript, jquery, python, and HTML/CSS so this i am doing for learning purpose.
I want you guys to please tell me the way how can i do it, just guide me rest of it i will handle.
Some similar examples are
http://themes.themepunch.com/?theme=megafoliopro_jq
http://codecanyon.net/item/dzs-scroller-gallery-cool-jquery-media-gallery/full_screen_preview/457913?ref=ibrandstudio
I am new to this forum and expect someone will answer my question
Thanks a lot mates.
Download CMS Made Simple and get either the album or gallery module. This is one out of many ways to get the job done.
You could glob() a directory to generate your thumbnails using PHP. I use this method on my photography site:
<?php
$counter = 0; // Set counter to 0
foreach (glob("images/photo-strip/thumb/*.jpg") as $pathToThumb) { // Grab files from the thumbnail path and save them to variable
$th_filename = basename($pathToThumb); // Strip the thumbnail filename from the path
$filename = str_replace('th_', '', $th_filename); // Strip th_ from the filename and save it to a different variable
$pathToFull = 'images/photo-strip/' . $filename; // Rebuild the path to the full-size image
echo ("<section class=\"photo-box\"><img src=\"$pathToThumb\" /></section>"); // Echo the photobox
$counter++; // Increment counter by 1 until no file exists
}
?>
You could expand on this code some in order to generate your "captions" perhaps even style your captions off a title="" property inside the <img> tag. How you match those captions up to the file is up to you.

PHP image gallery- dealing with iPhone / iPad images

Ive created an image gallery with php and mysql, incorporating several ways to add images & ability to sort by addition method and/or category. After noticing some images from apple devices showed up with the 'wrong' orientation, I created another page to edit orientation and other file info, then save said changes back to file and db. Only after I thought I had solved this problem did I view the altered images on an apple device, only to realize that image was now in 'wrong' orientation on said device. I've been googling this, but can't quite figure out exactly what I need to learn now to deal with images from apple devices in this situation. A shove in the right direction would be greatly appreciated.
Thanks!
Seeing the same problem on my "gallery3" photo gallery. Like machouinard, I am using "jhead -norot filename.jpg" to strip the orientation header from the images. This fixes the Apple rotation problem, and it does not seem to mess up the other browsers.
To edit a bunch of the files in place, I go directly to the album storage area in (gallerytop)/lib/albums and do find -type f | xargs sudo jhead -norot. It is smart enough to only modify the files that need to be modified, and it'll print out a list of them to stdout as it is working.
To get gallery3 to update the thumbnails, I go into the database and set the "dirty" flags like this: echo "update items set thumb_dirty=1,resize_dirty=1 where relative_path_cache like 'ALBUMNAME/%';" | mysql -u root -p gallery3 . Then I go into the gallery maintenance mode and run the "rebuild images" utility.
Forgot I posted this. I figured out how to handle the orientation issue so I thought I'd share. Seems simple now. I've since recreated this project using Codeigniter. CI is great and saves a lot of time, but I'm glad I wrote it all myself the first time. I sure learned more that way.
First, if the file is a jpg, I get the EXIF data with PEL .
$new is the uploaded file to be checked for orientation.
$this->load->library('pel/PelJpeg');
if($ext == 'jpg'){
$pdw= new PelDataWindow(file_get_contents($new));
if(PelJpeg::isValid($pdw)){
$input_jpg = new PelJpeg($new);
$exif = $input_jpg->getExif();
}
}
Then if EXIF exists, get the orientation value and run it through a switch statement, rotate it accordingly and then reset the orientation value. I'm using image_moo and Codeigniter, but this can obviously be changed to use any image manipulation library.
I'm honestly not sure if all those IF statements need to be there, but I kept running into trouble with jpg's that only included some EXIF info and would blow up the script without them.
if($exif !== NULL){
if($tiff = $exif->getTiff()){
if($ifd0 = $tiff->getIfd()){
if($orient = $ifd0->getEntry(PelTag::ORIENTATION)){
$this->image_moo->load($new);
//find the orientation value from the orientation tag. May be a better way, but this works for me.
$orientation = str_replace(' ', '', $orient);
//The orientation value from the orientation tag is after 'Value:'
if (($tmp = strstr($orientation, 'Value:')) !== false) {
$str = substr($tmp, 6, 1);
}
switch ($str)
{
// up is pointing to the right
case 8:
$this->image_moo->rotate(90);
$orient->setValue(1);
break;
// image is upside-down
case 3:
$this->image_moo->rotate(180);
$orient->setValue(1);
break;
// up is pointing to the left
case 6:
$this->image_moo->rotate(270);
$orient->setValue(1);
break;
// correct orientation
case 1:
break;
}
$this->image_moo->save($new,TRUE);
if ($this->image_moo->errors) print $this->image_moo->display_errors();
$this->image_moo->clear();
}
}
}
}
Hope this will be helpful to someone else struggling with the same issues.
If you see anything that could be improved, please let me know. But this works great for me.
Mark

Automatically generate <img> and then insert it into a <html> page after uploading the im into an FTP folder?

This is a bit of a shot int he dark, but i dont even know how to google this to get started.
It's a very simple idea, but it would be extremely useful to designers and creatives for creating a scrapbook of sorts.
Baiscally i want to be able to drop an image I find on the internet onto an FTP folder which will just be a droplet on my desktop. The img gets uploaded to the folder. Then a Chron job on the server (I'm assuming that's what i'd need to set up?) checks to see if any new images are up, and then automatically creates code for the image and updates a single html file.
Really simple. But how would one go about that? PHP? I really dont know where to start as I'm only ok at a bit of jQuery and templating wordpress.
I mean maybe I could do this in wordpress too. That it just checks to see if any images are in the uploads folder and then creates a single post with no titles, comments and all that BS, but just inserts the image.
Any pointers would be immensely appreciated!
Best,
Marc
As I understand you correctly, you just want to have a page showing every image in a specific folder. That can easily be done with the glob() function:
foreach(glob('folder_with_images/*.jpg') as $image){
echo '<img src="'.$image.'" alt="" />';
}
That's all. Just set up the "query" for the globas you need it. You don't need to do any kind of cronjob or update. It simply show all available images "live" as you open the page. I use glob() and not opendir() because you can use filter like "*.jpg" which you can't do with opendir().
When you have multiple file types, you can use scandir() and a if-statement:
foreach (scandir('folder_with_images/') as $image) {
if(substr($image, -3) == 'jpg' || substr($image, -3) == 'gif' || substr($image, -3) == 'png'){
echo '<img src="'.$image.'" alt="" />';
}
}
Check out the function opendir in php :
http://us.php.net/opendir
You can just have a php page that does opendir on your ftp directory, then spits out an img tag for each .jpg file in the directory. This can read teh directory each time you open the page.

How to save a phpthumb output into a file?

I am trying to save PhpThumb output. As what I could find on-line was not sufficient or too complex, I would like to ask if any one knows how to it?
$thumb_src="\"phpThumb/phpThumb.php?src=../apartmentsPhotos/".$num['ref']."/1.JPG&h=119&q=100\"";
echo" '<'img src=".$thumb_src />";
So what I want to do is to save the img src into an Image.
(So far I was creating the thumbnails on the fly but it seems that google and my web server donĀ“t like it too much. Saving the thumbnails will ensure that in no time I will have all my thumbnails in real files and then I will use this function just for new content.)
From phpThumb's FAQ
The best way is to call phpThumb as an object and call RenderToFile() to save the
thumbnail to whatever filename you want. See /demo/phpThumb.demo.object.php for an example. The other way is to use the 'file' parameter (see /docs/phpthumb.readme.txt) but this parameter is deprecated and does not work in phpThumb v1.7.5 and newer.
Once you have generated the URL with this line you posted:
$thumb_src="\"phpThumb/phpThumb.php?src=../apartmentsPhotos/".$num['ref']."/1.JPG&h=119&q=100\"";
Pass it as a $_GET variable to another page, call it serveThumb.php:
if (!isset($_GET['img']))
exit;
header('Content-type: application/pdf');
echo file_get_contents($_GET['img']);
You might have to add your own validation to serveThumb.php. Now you can save the result of serveThumb.php as a JPG.
Alternatively, save the contents of the image as a JPG file.
if (!isset($_GET['img']))
exit;
$img = file_get_contents($_GET['img']);
file_put_contents("myImage.jpg", $img);

Categories