/** * Moves an image file (resizing is possible) * * @param string $source_file Path of the source file * @param string $target_file Path of the target file * @param int $resize_width Width to resize * @param int $resize_height Height to resize * @param string $target_type If $target_type is set (gif, jpg, png, bmp), result image will be saved as target type * @param string $thumbnail_type Thumbnail type(crop, ratio) * @return bool true: success, false: failed **/ function createImageFile($source_file, $target_file, $resize_width = 0, $resize_height = 0, $target_type = '', $thumbnail_type = 'crop') { $source_file = FileHandler::getRealPath($source_file); $target_file = FileHandler::getRealPath($target_file); if (!file_exists($source_file)) { return; } if (!$resize_width) { $resize_width = 100; } if (!$resize_height) { $resize_height = $resize_width; } // retrieve source image's information $imageInfo = getimagesize($source_file); if (!FileHandler::checkMemoryLoadImage($imageInfo)) { return false; } list($width, $height, $type, $attrs) = $imageInfo; if ($width < 1 || $height < 1) { return; } switch ($type) { case '1': $type = 'gif'; break; case '2': $type = 'jpg'; break; case '3': $type = 'png'; break; case '6': $type = 'bmp'; break; default: return; break; } // if original image is larger than specified size to resize, calculate the ratio if ($resize_width > 0 && $width >= $resize_width) { $width_per = $resize_width / $width; } else { $width_per = 1; } if ($resize_height > 0 && $height >= $resize_height) { $height_per = $resize_height / $height; } else { $height_per = 1; } if ($thumbnail_type == 'ratio') { if ($width_per > $height_per) { $per = $height_per; } else { $per = $width_per; } $resize_width = $width * $per; $resize_height = $height * $per; } else { if ($width_per < $height_per) { $per = $height_per; } else { $per = $width_per; } } if (!$per) { $per = 1; } // get type of target file if (!$target_type) { $target_type = $type; } $target_type = strtolower($target_type); // create temporary image with target size if (function_exists('imagecreatetruecolor')) { $thumb = imagecreatetruecolor($resize_width, $resize_height); } else { if (function_exists('imagecreate')) { $thumb = imagecreate($resize_width, $resize_height); } else { return false; } } if (!$thumb) { return false; } $white = imagecolorallocate($thumb, 255, 255, 255); imagefilledrectangle($thumb, 0, 0, $resize_width - 1, $resize_height - 1, $white); // create temporary image having original type switch ($type) { case 'gif': if (!function_exists('imagecreatefromgif')) { return false; } $source = @imagecreatefromgif($source_file); break; // jpg // jpg case 'jpeg': case 'jpg': if (!function_exists('imagecreatefromjpeg')) { return false; } $source = @imagecreatefromjpeg($source_file); break; // png // png case 'png': if (!function_exists('imagecreatefrompng')) { return false; } $source = @imagecreatefrompng($source_file); break; // bmp // bmp case 'wbmp': case 'bmp': if (!function_exists('imagecreatefromwbmp')) { return false; } $source = @imagecreatefromwbmp($source_file); break; default: return; } // resize original image and put it into temporary image $new_width = (int) ($width * $per); $new_height = (int) ($height * $per); if ($thumbnail_type == 'crop') { $x = (int) ($resize_width / 2 - $new_width / 2); $y = (int) ($resize_height / 2 - $new_height / 2); } else { $x = 0; $y = 0; } if ($source) { if (function_exists('imagecopyresampled')) { imagecopyresampled($thumb, $source, $x, $y, 0, 0, $new_width, $new_height, $width, $height); } else { imagecopyresized($thumb, $source, $x, $y, 0, 0, $new_width, $new_height, $width, $height); } } else { return false; } // create directory $path = dirname($target_file); if (!is_dir($path)) { FileHandler::makeDir($path); } // write into the file switch ($target_type) { case 'gif': if (!function_exists('imagegif')) { return false; } $output = imagegif($thumb, $target_file); break; case 'jpeg': case 'jpg': if (!function_exists('imagejpeg')) { return false; } $output = imagejpeg($thumb, $target_file, 100); break; case 'png': if (!function_exists('imagepng')) { return false; } $output = imagepng($thumb, $target_file, 9); break; case 'wbmp': case 'bmp': if (!function_exists('imagewbmp')) { return false; } $output = imagewbmp($thumb, $target_file, 100); break; } imagedestroy($thumb); imagedestroy($source); if (!$output) { return false; } @chmod($target_file, 0644); return true; }