Categories
Linux shell

CLI Thumbnail generation

[et_pb_section admin_label=”Section” fullwidth=”on” specialty=”off”][et_pb_fullwidth_post_title admin_label=”Fullwidth Post Title” title=”on” meta=”on” author=”off” date=”on” categories=”on” comments=”off” featured_image=”on” featured_placement=”background” parallax_effect=”off” parallax_method=”on” text_orientation=”center” text_color=”dark” text_background=”on” text_bg_color=”rgba(255,255,255,0.9)” module_bg_color=”rgba(255,255,255,0)” title_all_caps=”off” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

[/et_pb_fullwidth_post_title][/et_pb_section][et_pb_section admin_label=”section”][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

Just found this snippet in one of my local snippet files. i think I got this from some blog…. but I can’t recall 🙁 anyhoo awesome little piece of code, generates thumbnails from all images in the dir its run from.

 

#!/bin/sh

make_thumbnail() {
    img=$1
    thumb=$(dirname "$1")/thumb-$(basename "$1")
    convert "$img" -thumbnail x100 "$thumb"
}

for pic in *.jpg
do
    make_thumbnail "$pic"
done

 

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_text admin_label=”bottom above footer” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

[/et_pb_text][et_pb_divider admin_label=”Divider” color=”#ffffff” show_divider=”off” divider_style=”solid” divider_position=”top” hide_on_mobile=”on”]

[/et_pb_divider][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”]

[jetpack-related-posts]

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

Categories
PHP

PHP Image Magick Drop Shadow

Want to add a simple drop shadow on any image? well here’s how to do it on the fly via PHP and Image Magick.

< ?php $img = new Imagick('images/image.jpg'); $img->setImageFormat('png');
    $shadowed = $img->clone();
    $drop_shadow = $img->clone();
    $drop_shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );
    $drop_shadow->shadowImage( 80, 3, 5, 5 );
    $drop_shadow->compositeImage( $img, Imagick::COMPOSITE_OVER, 0, 0 );
    header( "Content-Type: image/jpeg" );
    echo $drop_shadow; 
?>

Here’s how to do same via PHP and Image Magick but writing the file out to a second image leaving your original alone.

<?php $filename_shdw = 'images/image_shadow.jpg'; if (!file_exists($filename_shdw)) { $img = new Imagick('images/image.jpg'); $img->setImageFormat('png');
    $shadowed = $img->clone();
    $drop_shadow = $img->clone();
    $drop_shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );
    $drop_shadow->shadowImage( 80, 3, 5, 5 );
    $drop_shadow->compositeImage( $img, Imagick::COMPOSITE_OVER, 0, 0 );
    $drop_shadow->writeImage( 'images/image_shadow.jpg' );
}
    echo ''; 
?>

these snippets are very simple, you can expand them, include them in your own code, randomize the drop shadow etc. I will expand this in another post.

Categories
Linux shell

Create Thumbnails from a bunch of images

Linux Code snippet. Ever wated to batch create thumbnails? “musclecarr” posted this bash script to create thumbnails of all images of a specific type. It uses imagemagick and specifically the convert command. minor modifications done to it and comments added for easy understanding, here it is :

#!/bin/bash
# take argument or use current dir
DIR=${1:-`pwd`}
# create dir thumbs if doesnt exist
[ -d "$DIR/thumbs" ] || mkdir "$DIR/thumbs"
# error out if cant enter dir for some reason (permissions?)
cd "$DIR" || { echo >&2 "error: couldn't enter directory: $DIR"; exit 1;}
# For every file with extension XXX (jpg in this case)
for i in ./*.jpg; do
# Resize image to 100x100
convert "$i" -resize 100x100 "$DIR/thumbs/$i"
# profit? nah.
done