Created
July 3, 2013 11:10
-
-
Save umidjons/5917079 to your computer and use it in GitHub Desktop.
Dynamically create thumbnails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ImgUtils { | |
/** | |
* Creates and returns image resource if type is supported. | |
* Currently supported types (JPEG, GIF, PNG) | |
* @param string $file URL to the file | |
* @return bool|resource image resource if image type supported, otherwise false | |
*/ | |
private static function OpenImage( $file ) | |
{ | |
$size = getimagesize( $file ); // detect type and process accordingly | |
switch ( $size[ "mime" ] ) { | |
case "image/jpeg": | |
$im = imagecreatefromjpeg( $file ); // jpeg file | |
break; | |
case "image/gif": | |
$im = imagecreatefromgif( $file ); // gif file | |
break; | |
case "image/png": | |
$im = imagecreatefrompng( $file ); // png file | |
break; | |
default: | |
$im = false; // not supported type | |
break; | |
} | |
return $im; | |
} | |
/** | |
* Resizes image and returns URL to the resized image (thumbnail). | |
* @param string $fileUrl URL to the file | |
* @param int $newWidth new width (smaller than original) | |
* @param int $newHeight new height (smaller than original) | |
* @return bool|string URL to the resized image (thumbnail) if success, otherwise false | |
*/ | |
public static function ResizeImage( $fileUrl, $newWidth = 150, $newHeight = 150 ) | |
{ | |
$image = self::OpenImage( $fileUrl ); // get image resource | |
if ( $image === false ) return false; | |
$imageW = imagesx( $image ); | |
$imageH = imagesy( $image ); | |
// calculate new image dimensions (preserve aspect) | |
if ( $newWidth && !$newHeight ) { | |
$new_w = $newWidth; | |
$new_h = $new_w * ( $imageH / $imageW ); | |
} | |
elseif ( $newHeight && !$newWidth ) { | |
$new_h = $newHeight; | |
$new_w = $new_h * ( $imageW / $imageH ); | |
} | |
else { | |
$new_w = $newWidth ? $newWidth : 150; | |
$new_h = $newHeight ? $newHeight : 150; | |
if ( ( $imageW / $imageH ) > ( $new_w / $new_h ) ) { | |
$new_h = $new_w * ( $imageH / $imageW ); | |
} | |
else { | |
$new_w = $new_h * ( $imageW / $imageH ); | |
} | |
} | |
$fileNewUrl = self::GetThumbnailPath( $fileUrl, $newWidth, $newHeight ); | |
$newImage = imagecreatetruecolor( $new_w, $new_h ); // create resource | |
imagecopyresampled( $newImage, $image, 0, 0, 0, 0, $new_w, $new_h, $imageW, $imageH ); // copy | |
imagejpeg( $newImage, $fileNewUrl ); // save | |
return $fileNewUrl; // return URL to the thumbnail | |
} | |
/** | |
* Generates thumbnail URL from original file URL and its dimensions. | |
* @param string $fileUrl URL to the file | |
* @param int $newWidth width of the thumbnail | |
* @param int $newHeight height of the thumbnail | |
* @return string URL to the thumbnail | |
*/ | |
public static function GetThumbnailPath( $fileUrl, $newWidth, $newHeight ) | |
{ | |
$pi = pathinfo( $fileUrl ); // get file URL parts | |
// create new URL for thumbnail, for ex in DIR/FILENAME_150x150.EXT format | |
$fileNewUrl = sprintf( "%s/%s_%dx%d.%s", $pi[ 'dirname' ], $pi[ 'filename' ], $newWidth, $newHeight, $pi[ 'extension' ] ); | |
return $fileNewUrl; | |
} | |
/** | |
* Generates if thumbnail not found, or found but regenerate equal to true and returns URL to the thumbnail. | |
* @param string $fileUrl URL to the file | |
* @param int $width width of the thumbnail | |
* @param int $height height of the thumbnail | |
* @param bool $regenerate if true forces regenerate thumbnail, otherwise returns existing one | |
* @return bool|string thumbnail URL if success, false otherwise | |
*/ | |
public static function GetThumbnail( $fileUrl, $width = 150, $height = 150, $regenerate = false ) | |
{ | |
$thumbPath = self::GetThumbnailPath( $fileUrl, $width, $height ); | |
if ( file_exists( $thumbPath ) && is_file( $thumbPath ) && !$regenerate ) { | |
user_error( 'not generated' ); | |
return $thumbPath; | |
} | |
else { | |
user_error( 'generated' ); | |
return self::ResizeImage( $fileUrl, $width, $height ); | |
} | |
} | |
} | |
?> | |
<?php | |
$myImagUrl = '/home/user1/www/uploads/images/myimage.jpg'; | |
$thumbnail = ImgUtils::GetThumbnail( $myImagUrl ); // return url to the 150x150 thumbnail, if not exists generates it | |
// $thumbnail = ImgUtils::GetThumbnail( $myImagUrl, 200, 200, true ); // return 200x200 thumbnail url forcing regenerate it | |
if ( $thumbnail ) { | |
?> | |
<img src="<?= str_replace( $_SERVER[ 'DOCUMENT_ROOT' ], '', $thumbnail ) ?>" alt=""/> | |
<? | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment