Create new JPG image thumbnail if original file has changed - md5-file

I have a simple PHP script using imagecreatefromjpeg to create a thumbnail version of original and save it to a new folder. To speed parsing (all done in the back end prior to upload to static site) I am using file_exist before the creation to check it exists and show - if not create new and show. This works fine but if the original image changes the script does not generate as the thumbnail image exists in it's old form.
I guess I need to use MD5 test but as a n00b not sure how to test if the resulting thumbnail version would be different from the existing as produced by the main image.
Current Logic:
if thumb exist == do nothing,
if thumb does not exist == create it
Additional logic
if thumb will change due to change in original == create it,
if thumb will stay the same == do nothing
The existing PHP is very poor and clobbered together so happy for any pointers from a fresh (expert) view.
Thanks, John

You want to check the time the file was modified.
http://php.net/manual/en/function.filemtime.php
if the original file's timestamp is newer than the thumbnail's timestamp, then make a new thumbnail.
Thanks guys - not sure if that will work if the original image was created previously but only just being used eg stock images that are on file elsewhere and then dragged into the /images/ folder to replace existing image but with the same filename.. does the timestamp show the date dragged or when it was created?
If you move the files, the modified timestamp does not change.
If you copy the files, the copies are created by the copy action.
When you create a file, its Date Modified timestamp is the same as its Date Created timestamp.
So if your drag is copying the files, then the modified timestamp is the timestamp of the moment that the files were copied.

It should be enough to compare the last modification date of the thumb and the original image. if the original image is newer than the thumb, you should recreate it.

In simple terms, say we have an image that is a 200x200 image filled with red and only red (so yeah a red square). We create 80x80 thumbnails. If the original image changes but is still just the same red filled square, the thumbnail will not change.
The thing is that in order to detect this you will need to create the thumbnail or apply advanced image comparison techniques, that you may as well save it.
You can however use MD5 to create a one-on-one map from original to thumbnail, by naming the thumbnails as the MD5 hash of the original. The drawback of this approach is that you will need a way to remove unused thumbnails when their originals are no longer used.
To use this approach use something like:
<?php
$thumbname = md5_file($bigimage_path);
if( !file_exists("images/thumbs/$thumbname.jpg") )
{
create_thumb($bigimage_path, $thumbname);
}
?>
This will accomplish that if you have the exact same big image as different filenames, they will all map to the same thumbnail.

Related

Cannot place custom items on client hotbar/toolbar

Whenever I make a custom item, I am unable to drag it onto a hotbar. When I try to pick it up the icon turns to a question mark and will not stick to a hotkey.
For example, I made an exact copy of the Murloc Costume (id 33079) at id 50017 (which was a free slot on my DB). The original I can put on a hotbar. The custom one I cannot.
Here's a gif of the issue
Answer: if it's not in the client side DBC's, it will not work properly.
From ReynoldsCahoon on the AC Discord:
I just modified a DBC recently. I had to use a tool (I used Ladik's MPQ Editor) to extract the specific DBC I wanted to modify, and then I used WDBX to open the DBC and manipulate it. In WDBX you can output it in a variety of formats (like CSV or SQL) so you can modify the values any way you like, and then reimport (via CSV or SQL) the values back in.
I loosely followed the guide here: https://model-changing.net/tutorials/article/23-41-creating-your-first-mpq-patch/
I exported a DBC containing all of the Character Titles in the game. Erased all of them, and then imported a bunch of new values of my own choosing. Instead of importing that back into the MPQ I got it from, I created a new MPQ called patch-4.mpq that I placed in my client WotLK/Data/ directory, and then on the server, I placed the DBC file into the worldserver/data/dbc/ directory, replacing the original DBC.
I think this can optionally be overwritten in the database, by using the associated _dbc table to override values from the dbc files (someone correct me if this part is wrong).

AE Extendscript layered source file

I'm working on a script that does find/replace for missing items in your project. Unfortunately I'm running into a situation detecting and then replacing layered image sources (psd, ai, etc.).
1) I see no way of detecting if a AvItem is a layer within a layered image other than parsing the item.name, which is unreliable because a user can always rename items in the project panel.
2) Once I do know that it is a part of a layered image I cannot figure out how to re-link it to the correct image without replacing the layer with the merged image. item.replace(new_path) will replace that item with the whole image, not the layer within the image. For example:
var item = app.project.item(3); //assuming this is the 'layer' we want to replace
item.replace(new_path);
So is there a secret property somewhere which will reliably tell me if an item is a part of a layered image, and if so is there a way to relink it without replacing the layer with the entire merged image?
EDIT
Here's a function to guess if a layer is part of a layered image. It's not bullet-proof but it should work as long as the user does not rename the item:
function isSourceLayered (av_item) {
// check if there is a "/"
if (av_item.name.indexOf("/") != -1) {
// check if it is in a "layers" folder
if (av_item.parentFolder.name.indexOf("Layers") != -1) {
return true;
}
}
return false;
}
I just asked the same question on the Adobe extendscript forum. Unless there's undocumented features (and I spent a bit of time looking with Extendscript Toolkit's data browser) the fileSource object doesn't seem to have any attributes or methods to do this.
There is a kind of a workaround, you can import the file using ImportOptions.importAs(ImportAsType.COMP) This will import a comp, and you can loop through the layers matching the name, get the source of that layer and use that as your new source. But as you say, it doesn't work if the source has been renamed.
I've written this into a function, it's available on github Edit: I forgot that I changed the way that function works. It doesn't re-import layer sources because of this problem, it just uses the Duplicate menu command.

Dynamically Update Manifest file based on image uploads in SilverStripe admin

I'm new to HTML5's application cache, but am familiar with the basics of it. I'm working on a cache manifest file for a SilverStripe site that needs to be dynamically updated whenever the content manager uploads a new image. I understand that the images will be appended to the Manifest function below using a loop, but the part that I find to be a challenge is updating the date and version number every time. Would I need to have the date and version listed as variables? Or is that not possible considering the setup of the Manifest function?
public function Manifest() {
$static = <<<EOT
CACHE MANIFEST
# 2016-03-17 v6.0.0
[manifest content]
EOT;
//Append any new image file that gets uploaded
$static = $static . "\n" . "/test.html";
$this->response->addHeader("Content-type", "text/cache-manifest");
return $static;
}
When you change cached by manifest file, the manifest content does not change. However you must change the content to trigger update in a browser. In this case you update a comment. This comment can contain anything. The date and version is common practice, because they reflect the change. So all you need it to get most recent change date from File.LastEdited field.

Converting RTF to PDF from System

I've createad a rule to transform any file to PDF and copy this one to another folder.
So i can add a file named: "test.rtf" and then the rule create a test.pdf into folder "PDF"..
Till here its ok. If i add a file through alfresco (add content button) it works perfectly...
By the way, on the system that i've developed when i try to add a file then i get my file .rtf correctly in the folder, but the pdf file converted and copied goes without any content...
If i send a rtf file with a table with 10 rows and into the rows i right "testing" then the pdf created goes with the table, and with 10 empty rows...
Someone knows the reason for that?
Im not sure, but maybe when i send the file by the system alfresco starts to convert and copy before completing to create the rtf... someone already got some problem like this one?
The problem you're getting in is that Alfresco first creates an empty file with all the meta-data and then updates the file with the associated content.
So you can do 2 things:
1: create a rule which is triggered on update, instead on create/inbound
2: create a rule which triggers a Javascript file, which will do the transformation and will check on the content size.
Hence it's better to create a rule which checks the content on size.
Create a JavaScript file in Data Dictionary/Scripts.
Check the JavaScript API
Something like this:
if (document != null && document.size > 0){
document.transformDocument("application/rtf");
}

Addiing captions above images in wordpress

So I added some code in my css and there are boxes that appear over every image that is attached to a post. I've wanted to number the images and show the image number in the box(1...n). I have this in my functions.php
edit: code was added here http://pastebin.com/gVszwf75
If I run only count_images it will show the correct number of attached images to a post(let's say 15). But for some reason the number that is shown in the boxes over the images is always 1. I've seen this done on several blogs with just php so there has to be a way(even if I have to change my whole code).
The problem with your code is that you're looping through the array each time you call your callback function caption_image_callback() ... it has no memory of how many times it's looped!
The easiest way to fix this is to add a global variable at the beginning of the plug-in, I call it $caption_image_count and set it equal to zero. Then call the variable in caption_image_callback() and increment by 1 each time you call the function. This will keep track of the number of captioned images you have on the page.
If you want, you can also re-set the variable to zero before you return $post_body_content in caption_image(). I've posted the full solution to your pastebin: http://pastebin.com/sFe6dhqL

Resources