image_functions.php
<?php
###############################################################################################################
/*
function image_size($image)
Determines size of the image to be displayed in pixels for HTML insertion
Parameters: $image = The image which size is to be determined
*/
function image_size($image){
$image = "images/{$image}.jpg"; // Get the image location given the name
$image_size = @getimagesize($image); // "@" supresses error msg (E_WARNING) if return False (e.g. img not found)
// getimagesize() returns an array (width,height,type,attributes) attributes contains the width ="??? height="???" for HTML image tag attributes
return $image_size[3];
}
###############################################################################################################
//=============================================================================================
//=============================================================================================
//
// function createthumb creates a thunbnail of an image
// @param ('initialImageName.jpg','tn_imageName.jpg',new_width,new_height);
//
//
// NOTE "@" IS SUPPRESSING "supplied argument is not a valid Image resource" WARNINGS
function createthumb($name,$filename,$new_w,$new_h){
$system=explode('.',$name);
if (preg_match('/jpg|jpeg|JPG|JPEG/',$system[1])){
$src_img=imagecreatefromjpeg($name);
}
if (preg_match('/png|PNG/',$system[1])){
$src_img=imagecreatefrompng($name);
}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
//call the function with this:
//createthumb('test.jpg','tn_apple.jpg',150,150);
//
//
//
//=============================================================================================
//=============================================================================================
//=============================================================================================
//=============================================================================================
//
// function imageResize($width, $height, $target)
//
// Resizes an image while maintaining the aspect ratio returning the new width and height.
function imageResize($width, $height, $target) {
//takes the larger size of the width and height and applies the
//formula accordingly...this is so this script will work
//dynamically with any size image
if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}
//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);
//returns the new sizes in html image tag format...this is so you
//can plug this function inside an image tag and just get the
return "width=\"$width\" height=\"$height\"";
}
/**
Call example:
$size = GetImageSize("galleries/$galleryID/$picture_name"); // Read the size
$width = $size[0];
$height = $size[1];
$dimentions2 = imageResize($size[0],$size[1], 300);
<img src="galleries/'.$galleryID.'/'.$picture_name.'" '.$dimentions2.' />
*/
//
//
//
//=============================================================================================
//=============================================================================================
?>
Paul's Code Library
Categories
HomeAJAX
classes
database_functions
date_functions
file_functions
googlevideo_and_youtube_api
htaccess
image_functions
string_functions