Wir hatten gestern ne Frage zum Thumbnails erstellen ...
hatte lange weile ...
free use/change/w/e
PHP
<?php
function ratioResize(array $src_size, array $dst_size=array(), $round_int=true){
/* parameter:
$src_size: (array) source size array(0 => (int){width}, 1 => (int){height})
$dst_size: (array) destination size array(0 => (int){width}, 1 => (int){height})
$round_int: (bool) round width and height
returns:
success:
array((numeric)$dst_width, (numeric)$dst_height)
failure:
false
*/
if(!isset($src_size[0])or !isset($src_size[1])){
return false;
}
$dst_width = (float)isset($dst_size[0]) ? $dst_size[0] : $src_size[0];
$dst_height = (float)isset($dst_size[1]) ? $dst_size[1] : $src_size[1];
if($src_size[0] <> $dst_width or $src_size[1] <> $dst_height){
$src_ratio = ((float)$src_size[0] > 0) ? $src_size[0] / $src_size[1] : 0;
if(((float)$dst_width > 0 ? $dst_width / $dst_height : 0) > $src_ratio){
$dst_width = $dst_height * $src_ratio;
}else{
$dst_height = ((float)$dst_width > 0) ? $dst_width / $src_ratio : 0;
}
}
if($round_int === true){
$dst_width = round($dst_width);
$dst_height = round($dst_height);
}
return array($dst_width, $dst_height);
}
function getValidFilenameFrom($filename){
return preg_replace('/[^a-zA-Z0-9_]/', '_', $filename);
}
function resizeImage($src_file, array $dst, array $dst_size=array(), &$ecode=null, &$emsgs=null)
{
/* parameter:
$src_file: (string) source path + filename -- path/path/filename.extension
$dst: (array) destination array(
0 => (string){path},
1 => (string){name},
2 => (string){format 'png', 'jpeg', 'gif' or 'wbmp'}
)
$dst_size: (array) resize image to array(0 => (int){width}, 1 => (int){height})
&$ecode: (int) error code
&$emsgs: (string) error message
returns: (bool)
*/
// check source file
$src_file = (string)$src_file;
if(!file_exists($src_file)){
$ecode = 10;
$emsgs = 'source filename "' . $src_file . '" does not exist';
return false;
}
if(!is_file($src_file)){
$ecode = 11;
$emsgs = 'source filename "' . $src_file . '" is not a file';
return false;
}
if(!is_readable($src_file)){
$ecode = 12;
$emsgs = 'source filename "' . $src_file . '" is not readable';
return false;
}
// check destination path
$dst_path = (string)$dst[0];
if(strlen($dst_path) > 0){
$dst_path = str_replace(array('/', '\\'), array(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), (string)$dst_path);
if(substr($dst_path, -1) !== DIRECTORY_SEPARATOR){
$dst_path .= DIRECTORY_SEPARATOR;
}
}
if(!file_exists($dst_path)){
$ecode = 20;
$emsgs = 'destination path "' . $dst_path . '" does not exist';
return false;
}
if(!is_dir($dst_path)){
$ecode = 21;
$emsgs = 'destination path "' . $dst_path . '" is not a directory';
return false;
}
if(!is_readable($dst_path)){
$ecode = 22;
$emsgs = 'destination path "' . $dst_path . '" is not readable';
return false;
}
// check destination filename
$dst_name = (string)$dst[1];
if($dst_name !== getValidFilenameFrom($dst_name)){
$ecode = 30;
$emsgs = 'invalid destination filename "' . $dst_name . '"';
return false;
}
if(strlen($dst_name) < 1){
$ecode = 31;
$emsgs = 'destination filename cannot be empty';
return false;
}
// check destination format (plus set callable $call)
$dst_format = strtolower((string)$dst[2]);
if($dst_format === 'png'){
$call = 'imagepng';
}
elseif($dst_format === 'jpeg'){
$call = 'imagejpeg';
}
elseif($dst_format === 'gif'){
$call = 'imagegif';
}
elseif($dst_format === 'wbmp'){
$call = 'imagewbmp';
}
else{
// wrong format given
$ecode = 40;
$emsgs = 'invalid destination format "' . $dst_format . '"';
return false;
}
// prepare destination size
$dst_size = array(
isset($dst_size[0]) ? $dst_size[0] : null,
isset($dst_size[1]) ? $dst_size[1] : null,
);
// get source image raw data
if(($src_raw=file_get_contents($src_file)) === false){
// unable to read source
$ecode = 100;
$emsgs = 'unable to read source filename "' . $src_file . '"';
return false;
}
// create new image from source
if(($src_image=imagecreatefromstring($src_raw)) === false){
// must be something else than a picture
$ecode = 101;
$emsgs = 'destination filename "' . $src_file . '" is not an image';
return false;
}
// get current size
$src_size = getimagesize($src_file);
// get new size
$dst_size = ratioResize($src_size, $dst_size, false);
// create new image
if(($dst_image=imagecreatetruecolor($dst_size[0], $dst_size[1])) === false){
$ecode = 102;
$emsgs = 'unable to create new true color image';
return false;
}
// transparency
if(imagesavealpha($dst_image, true) === false){
$ecode = 103;
$emsgs = 'unable to set flag to save full alpha channel information';
return false;
}
if(($transparency = imagecolorallocatealpha($dst_image, 0, 0, 0, 127)) === false){
$ecode = 104;
$emsgs = 'unable to allocate a color for an image';
return false;
}
imagefill($dst_image, 0, 0, $transparency);
// copy and resize
if(
imagecopyresampled(
$dst_image, $src_image, 0, 0, 0, 0, $dst_size[0], $dst_size[1], $src_size[0], $src_size[1]
) === false
){
$ecode = 105;
$emsgs = 'unable to copy and resize image';
return false;
}
// write
if(call_user_func_array($call, array($dst_image, $dst_path . $dst_name . '.' . $dst_format)) === false){
// unable to write file
$ecode = 106;
$emsgs = 'unable to write image to destination';
return false;
}
imagedestroy($src_image);
imagedestroy($dst_image);
return true;
}
// example:
$source_image = 'path/path\\path/orig_img.jpg'; // a valid path + filename
$destination_path = 'path/path'; // a valid path
$destination_filename = 'myNewImage_1'; // a valid filename
$destination_format = 'png'; // 'png', 'jpeg', 'gif' or 'wbmp'
$destination_size_width = 50; // numeric
$destination_size_height = 50; // numeric
$ecode = null; // gets changed by ref on error
$emsgs = null; // gets changed by ref on error
$result = resizeImage(
$source_image,
array($destination_path, $destination_filename, $destination_format),
array($destination_size_width, $destination_size_height),
$ecode,
$emsgs
);
if($result === false){
echo 'ERROR: ' . $emsgs . ' [ecode ' . $ecode . ']';
}
else{
echo 'OK';
}
?>
Alles anzeigen