Quantcast
Channel: Moishe BeshkinTag Archive » Moishe Beshkin

Dynamic image rotation

$
0
0

I consider two basic logics on the idea how to rotate images on the site.

  • Change image every time site is loaded. This logics can be realized with the simple php script.
    $results = array();
    $handler = opendir("banner");
    
    while ($file = readdir($handler)) {
            if ($file != "." && $file != "..") {
                       $results[] = $file;
            }
    }
    $banner_num = rand(0, count($results)-1);
    print '';
    

    In this example, we just place all needed pictures to directory banner/. Script reads the list of files from the directory and converts the list to an array. Then we randomly take index of array item and show it on the page.

  • Another way is to use simple jquery plugin for rotating images.
    Here is a wonderful simple jQuery script for rotating images on site.
    We can combine previous php example and this script.
    $results = array();
    $handler = opendir("banner");
    print '
    ';

    Then get needed files from here.
    Place the following code in

    
    
    
    
    

    At the end of file (before ) add the following code:

    
    Note: On IE7 there appear floating caption section even in case there is no caption set. To resolve this issue, add "height: '0px'" as parameter.
    


Function for creating thumbnails in php

$
0
0

It took me long time to create an universal function which would create thumbnails from pictures and suit all my needs.
The base for this function, I took from here.
But I modified it slightly.

function createthumb($name, $newname, $new_w, $new_h, $by_small=true, $border=false, $transparency=true, $base64=false) {
    if(file_exists($newname))
        @unlink($newname);
    if(!file_exists($name))
        return false;
    $arr = split("\.",$name);
    $ext = $arr[count($arr)-1];

    if(preg_match('/jpeg/i', $ext)){
        $img = @imagecreatefromjpeg($name);
    }elseif (preg_match('/jpg/i', $ext)){
        $img = @imagecreatefromjpeg($name);
    } elseif(preg_match('/png/i', $ext)){
        $img = @imagecreatefrompng($name);
    } elseif(preg_match('/gif/i', $ext)) {
        $img = @imagecreatefromgif($name);
    }
    if(!$img)
        return false;
    $old_x = imageSX($img);
    $old_y = imageSY($img);
    if($old_x  $old_y) {
        if ($by_small) {
                $thumb_w = floor($old_x*($new_w/$old_y));
                $thumb_h = $new_h;
        }
    } elseif ($old_x == $old_y) {
        $thumb_w = $new_w;
        $thumb_h = $new_h;
    }
    $thumb_w = ($thumb_w= 0) {
                //its transparent
                $trnprt_color = imagecolorsforindex($img, $trnprt_indx);
                $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                imagefill($new_img, 0, 0, $trnprt_indx);
                imagecolortransparent($new_img, $trnprt_indx);
            }
        }
    } else {
        Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));
    }

    imagecopyresampled($new_img, $img, 0,0,0,0, $thumb_w, $thumb_h, $old_x, $old_y);
    if($border) {
        $black = imagecolorallocate($new_img, 0, 0, 0);
        imagerectangle($new_img,0,0, $thumb_w, $thumb_h, $black);
    }
    if($base64) {
        ob_start();
        imagepng($new_img);
        $img = ob_get_contents();
        ob_end_clean();
        $return = base64_encode($img);
    } else {
        if(preg_match('/jpeg/i', $ext)) {
                imagejpeg($new_img, $newname);
            $return = true;
        } elseif( preg_match('/jpg/i', $ext)){
            imagejpeg($new_img, $newname);
            $return = true;
        } elseif(preg_match('/png/i', $ext)){
            imagepng($new_img, $newname);
            $return = true;
        } elseif(preg_match('/gif/i', $ext)) {
            imagegif($new_img, $newname);
            $return = true;
        }
    }
    imagedestroy($new_img);
    imagedestroy($img);
    return $return;
}

So, if you want to create a thumbnail, so that the thumbnail size will be base on smallest side, then do the following:

createthumb($targetFile_big,$targetFile, 170, 170);

In this case the smallest part will become 170px and the biggest proportionally bigger. Otherwise:

createthumb($targetFile_big,$targetFile, 170, 170, false);

In this case the biggest part will become 170px and the smallest proportionally less.

Bash script to resize images

$
0
0

To make sure that this script works correctly, check that ImageMagic pack is installed on your Linux machine.

#! /bin/bash

dir=$1
target_dir=$2

mkdir -p $target_dir

list=`ls -1 $1`
for i in $list
do
        echo $i
        convert $dir/$i -resize 50% $target_dir/$i
done

Usage:

./convert_50.sh Pictures/temp/ Pictures/temp_new

This will resize all images in location Pictures/temp/ for 50% and will copy them to new location Pictures/temp_new





Latest Images