#native_company# #native_desc#
#native_cta#

Progressive Image Switcher – Cropper

By Tony Thomas
on August 4, 2005

Version: 1.0

Type: Function

Category: Graphics

License: GNU General Public License

Description: Change Progessive Images to Non-Progressive Images and Thumbnails for Flash applications. This script can also be used to dynamically create cropped thumbnails for any application.

This script was only written for Jpeg images, because JPEG images that are progressive can not be displayed in Flash applications using ActionScript. I’m sure it can be enhanced for other Image types.

<?php
// --------------------------------------------------------
// 
// Title: Progressive Image Switcher - Cropper
// Version: 1.0
//
// Use:
// Change Progessive Images to Non-Progressive Images and Thumbnails
// for Flash applications.  This script can also be used to dynamically
// create cropped thumbnails for any application.
//
// Created by: MercuryLevel
// Author: Tony Thomas
// email: [email protected]
// Aug 4, 2005
// 
// Copyright (C) 2005  Tony Thomas
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//
// -------------------------------------------------------------------------------
//
// Server Requirements:
// Created and tested on Linux Server with PHP 4.3.2 and GD Version 2.0.12
// 
// Instructions:
// - Create a blank jpg image the same size as the clipping area, and call it non.jpg
// - Place non.jpg in the same folder and the script calling the fucntion
// - Include the script in your program, and call the function with the imagename and path
// - Set the Clipping area and Thumbnail size fields ($ca and $ts)
// - Results will return a thumbnail image
//
// Limitations of this version:
// This script was only written for Jpeg images, because JPEG images that are progressive
// can not be displayed in Flash applications using ActionScript.
//
// Uses: Great for PHP and Flash projects, Image Galleries, Product Catalogs, etc.
//
// Use as you see fit. Add parameters, functions, other image types, what ever you need for both 
// personal or commercial use.
//
// If you use this script or portions of it, please keep the copyright information intact,
// add your comments, updated version number, and please email me the new version. Let me know
// how you used the script. ENJOY!
//
// Tony Thomas - [email protected]
// http://www.mercurylevel.com
//
// View the General Public Licence at:
// http://www.gnu.org/licenses/gpl.txt
//
// -------------------------------------------------------------------------------




function setImage($ImageFilenPath) {

  // set clipping_area
  $ca = 200;

  // set thumbnail_size
  $ts = 30;




  // only use .jpg's as the source file
  // Source should include image path information relative to the script
  // -------------------------------------------------------------------
  $image_text_name = $ImageFilenPath;



  // Get the name of the JPEG
  // ----------------------------------
  $bs_len = strlen($image_text_name);
  $bs_cut = $bs_len - 4;



  // Create the matching thumbnail image name
  // ----------------------------------
  $newThm = substr($image_text_name,0,$bs_cut) . "_thm.jpg";



  // get the full image, width and height
  // ----------------------------------
  $fullimage = imagecreatefromjpeg($image_text_name);
  $f_width = imagesx($fullimage);
  $f_height = imagesy($fullimage);



  // copy the image and resample for flash, keeping $fullimage for later use
  // ----------------------------------
  $newfull = Imagecreatetruecolor($f_width, $f_height);
  imagecopyresampled($newfull, $fullimage, 0, 0, 0, 0, $f_width, $f_height, $f_width, $f_height);
  imagejpeg($newfull,$image_text_name,100);
  imagedestroy($newfull);



  // check width and height for to see if it is worth clipping from the center
  // anything over 3 pixels of the clipping size (200) will be clipped from the center 
  // ----------------------------------

  $ay1 = ($f_height - $ca)/2;
  if($ay1 < 2){
    $clip_y = 0;
  } else {
    $clip_y = intval($ay1);
  }


  $ax1 = ($f_width - $ca)/2;
  if($ax1 < 2){
    $clip_x = 0;
  } else {
    $clip_x = intval($ax1);
  }




  // check width and height to see which is larger, then check the smaller for clippable area
  // if not enough pixels to clip then use the smaller size as the basis of the square
  // eg. Width=300   Height=180   Clipping-area=200
  // The result would be a square 180 pixels, later resized to 30
  // ----------------------------------

  if($f_width >> $f_height){
    if($f_height >> $ca) {
      $new_width = $ca;
      $new_height = $ca;
  
    } else {
      $new_width = $f_height;
      $new_height = $f_height;
    }
  } else {
    if($f_width >> $ca) {
      $new_width = $ca;
      $new_height = $ca;
    } else {
      $new_width = $f_width;
      $new_height = $f_width;
    }
  }




  // use blank non.jpg image (same size as clipping area) as the image to copy to
  // The nice thing about the blank image is that it is resized to fit any smaller images
  // ----------------------------------

  $tempImg = imagecreatefromjpeg("non.jpg");
  imagecopy($tempImg, $fullimage, 0, 0, $clip_x, $clip_y, $new_width, $new_height);
  imagejpeg($tempImg,$newThm,100);
  imagedestroy($tempImg);
  imagedestroy($fullimage);




  // Create the new thumbnail and resample it for Flash
  // ----------------------------------

  $srcImage = imagecreatefromjpeg($newThm);
  $destImage = Imagecreatetruecolor($ts, $ts);
  imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $ts, $ts, $new_width, $new_height);
  imagejpeg($destImage,$newThm,100);
  imagedestroy($srcImage);
  imagedestroy($destImage);

  return $newThm;

}


$myimage = setImage("cordova5.jpg");
echo "<img src="".$myimage."">";



?>