PHP 在等效位置自动生成缩略图(除非已手动创建)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 在等效位置自动生成缩略图(除非已手动创建)相关的知识,希望对你有一定的参考价值。

<?php

	set_error_handler('showBrokenImage');

	$imagesFolder = 'images/'; // Change this if required to point to your source folder
	$brokenImage = $imagesFolder.'brokenimage.png';
	$thumbnailHeight = $thumbnailWidth = 80; // Set to the max size of your thumbnail
	$previewHeight = $previewWidth = 250; // Set to the max size of your preview images
	date_default_timezone_set('Australia/NSW'); // Remove or update this line depending on your PHP.ini settings

	try {
		if(empty($_SERVER['PATH_INFO']) || strpos($_SERVER['PATH_INFO'], ':') !== false || strpos($_SERVER['PATH_INFO'], '..') !== false) {
			throw new exception('Invalid path');
		}
	
		$pathComponents = explode('/', strtolower(trim((isset($_SERVER['PATH_INFO']) && strlen(trim($_SERVER['PATH_INFO'], '/')) ? $_SERVER['PATH_INFO'] : ''), '/')));
	
		if($pathComponents[0] === 'thumb') {
			$outputWidth = $thumbnailWidth;
			$outputHeight = $thumbnailHeight;
		} else {
			$outputWidth = $previewWidth;
			$outputHeight = $previewHeight;
		}
	
		function mkdir_recursive($pathname, $mode = 0777) {
			is_dir(dirname($pathname)) || mkdir_recursive(dirname($pathname), $mode);
			return is_dir($pathname) || @mkdir($pathname, $mode);
		}
	
		$file = $imagesFolder.trim(substr($_SERVER['PATH_INFO'], strlen('/'.$pathComponents[0])), '/');
		$fileExtension = strtolower(substr($file, strrpos($file, '.') + 1));
		
		if(!is_file($file)) {
			throw new exception('Invalid path');
		}
	
		if(in_array($pathComponents[0], array('thumb', 'preview'))) {
			// Generate a smaller image if we haven't already got one
			$thumbFile = str_replace($imagesFolder, $imagesFolder.($pathComponents[0] === 'thumb' ? 'thumbnails' : 'previews').'/', $file);
			if(!file_exists($thumbFile) && file_exists($file)) {
				// Generate a thumbnail
				switch($fileExtension) {
					case 'jpg':
					case 'jpeg':
						$sourceImage = imagecreatefromjpeg($file);
						break;
					
					case 'png':
						$sourceImage = imagecreatefrompng($file);
						break;
				}
				
				$sourceX = imagesx($sourceImage);
				$sourceY = imagesy($sourceImage);
				if ($sourceX > $sourceY) {
					$destinationWidth = $outputWidth;
					$destinationHeight = $sourceY * ($outputHeight/$sourceX);
				} else if ($sourceX < $sourceY) {
					$destinationWidth = $sourceX * ($outputWidth/$sourceY);
					$destinationHeight = $outputHeight;
				} else if ($sourceX == $sourceY) {
					$destinationWidth = $outputWidth;
					$destinationHeight = $outputHeight;
				}
	
				$outputImage = imagecreatetruecolor($destinationWidth, $destinationHeight);
				imagecopyresampled($outputImage, $sourceImage, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $sourceX, $sourceY);
				
				// Make sure the destination folder path exists
				mkdir_recursive(dirname($thumbFile));
				
				switch($fileExtension) {
					case 'jpg':
					case 'jpeg':
						imagejpeg($outputImage, $thumbFile);
						break;
					
					case 'png':
						imagepng($outputImage, $thumbFile);
						break;
				}
			}
			
			$file = $thumbFile;
		}
	
		if(!file_exists($file)) {
			outputImage($brokenImage);
		}

		outputImage($file);
	} catch(exception $ex) {
		outputImage($brokenImage);
	}

	function outputImage($file) {
		error_reporting(0);
		$fileExtension = strtolower(substr($file, strrpos($file, '.') + 1));

		header('Last-Modified: '.@date('r', filemtime($file)));
		header('Accept-Ranges: bytes');		
		switch($fileExtension) {
			case 'jpg':
			case 'jpeg':
				header('Content-Type: image/jpeg');
				break;
			
			case 'png':
				header('Content-Type: image/png');
				break;
		}
		header('Content-Length: '.filesize($file));
		@ob_clean();
		flush();
		readfile($file);
		exit;
	}


	function showBrokenImage($errno, $errstr) {
		global $brokenImage;
	
		if(!in_array($errno, array(0, E_STRICT, E_NOTICE))) {
			outputImage($brokenImage);
		}

		return true; // Don't execute PHP internal error handler
	}

?>

以上是关于PHP 在等效位置自动生成缩略图(除非已手动创建)的主要内容,如果未能解决你的问题,请参考以下文章

使用 PHP 从 URL 创建缩略图

保存图片到数据库 生成缩略图

PHP-生成缩略图和添加水印图-学习笔记

PHP-生成缩略图和添加水印图-学习笔记

使用 PHP 获取 PDF 文件的高度和宽度

为啥我的 PHP 脚本在生成缩略图时会停止?