File: fwphp/01mater/shop/ch24_guitar_shop/util/images.php

Recommend this page to a friend!
  Classes of Slavko Srakocic  >  B12 PHP FW  >  fwphp/01mater/shop/ch24_guitar_shop/util/images.php  >  Download  
File: fwphp/01mater/shop/ch24_guitar_shop/util/images.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: B12 PHP FW
Manage database records with a PDO CRUD interface
Author: By
Last change:
Date: 1 month ago
Size: 3,429 bytes
 

Contents

Class file image Download
<?php
function process_image($dir, $filename) {
   
// Set up the variables
   
$i = strrpos($filename, '.');
   
$image_name = substr($filename, 0, $i);
   
$ext = substr($filename, $i);

   
// Set up the read path
   
$image_path = $dir . $filename;

   
// Set up the write paths
   
$image_path_m = $dir . $image_name . '_m' . $ext;
   
$image_path_s = $dir . $image_name . '_s' . $ext;

   
// Create an image that's a maximum of 400x300 pixels
   
resize_image($image_path, $image_path_m, 400, 300);

   
// Create a thumbnail image that's a maximum of 100x100 pixels
   
resize_image($image_path, $image_path_s, 100, 100);
}

/*******************************************
 * Resize image to 400x300 max
 ********************************************/
function resize_image($old_image_path, $new_image_path,
       
$max_width, $max_height) {

   
// Get image type
   
$image_info = getimagesize($old_image_path);
   
$image_type = $image_info[2];

   
// Set up the function names
   
switch($image_type) {
        case
IMAGETYPE_JPEG:
           
$image_from_file = 'imagecreatefromjpeg';
           
$image_to_file = 'imagejpeg';
            break;
        case
IMAGETYPE_GIF:
           
$image_from_file = 'imagecreatefromgif';
           
$image_to_file = 'imagegif';
            break;
        case
IMAGETYPE_PNG:
           
$image_from_file = 'imagecreatefrompng';
           
$image_to_file = 'imagepng';
            break;
        default:
            echo
'File must be a JPEG, GIF, or PNG image.';
            exit;
    }

   
// Get the old image and its height and width
   
$old_image = $image_from_file($old_image_path);
   
$old_width = imagesx($old_image);
   
$old_height = imagesy($old_image);

   
// Calculate height and width ratios
   
$width_ratio = $old_width / $max_width;
   
$height_ratio = $old_height / $max_height;

   
// If image is larger than specified ratio, create the new image
   
if ($width_ratio > 1 || $height_ratio > 1) {

       
// Calculate height and width for the new image
       
$ratio = max($width_ratio, $height_ratio);
       
$new_height = round($old_height / $ratio);
       
$new_width = round($old_width / $ratio);

       
// Create the new image
       
$new_image = imagecreatetruecolor($new_width, $new_height);

       
// Set transparency according to image type
       
if ($image_type == IMAGETYPE_GIF) {
           
$alpha = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
           
imagecolortransparent($new_image, $alpha);
        }
        if (
$image_type == IMAGETYPE_PNG || $image_type == IMAGETYPE_GIF) {
           
imagealphablending($new_image, false);
           
imagesavealpha($new_image, true);
        }

       
// Copy old image to new image - this resizes the image
       
$new_x = 0;
       
$new_y = 0;
       
$old_x = 0;
       
$old_y = 0;
       
imagecopyresampled($new_image, $old_image,
                          
$new_x, $new_y, $old_x, $old_y,
                          
$new_width, $new_height, $old_width, $old_height);

       
// Write the new image to a new file
       
$image_to_file($new_image, $new_image_path);

       
// Free any memory associated with the new image
       
imagedestroy($new_image);
    } else {
       
// Write the old image to a new file
       
$image_to_file($old_image, $new_image_path);
    }
   
// Free any memory associated with the old image
   
imagedestroy($old_image);
}

?>

For more information send a message to info at phpclasses dot org.