构建一个ConvertImage()函数,该函数取自我多年以来在网站上使用的部分代码,它应进行转换(如果是BMP或GIF),并调整要作为数据提供给它的图像的大小串。它正在工作,并且图像以适当的大小返回,但为空白。显然我错过了一些重要的事情,但是呢?
请注意,这不是将图像上传到任何地方,也没有将其插入数据库列中,至少目前没有。其目的仅仅是调整大小和转换它,不幸的是ImageMagick尚不可用。
为了进行测试,一个简单的表单将文件系统中的图像提交到此文件,该文件系统也调用该函数(有关参数的详细信息,请参见函数中的注释:]
<?php include_once("/var/www/html/site.loc/functions.php");
if (isset($_FILES['fileToProcess'])) :
$file = $_FILES['fileToProcess'];
$image = file_get_contents($file['tmp_name']);
$image = ConvertImage($image, 250, "Width");
ShowImage($image);
endif;
?>
然后是这些功能:
// Converts images to jpg (or to png if gif) and resizes them
// $MaxDim is a value for final width or height of the image. Use $SizeCentric to specify.
function ConvertImage($ImgFile, $MaxDim, $SizeCentric) {
global $Message;
list($ImgWidth, $ImgHeight, $ImgType, $Attr) = getimagesizefromstring($ImgFile);
if ($ImgType) :
switch ($ImgType) :
case 1 : $src = imagecreatefromgif($ImgFile); break;
case 2 : $src = imagecreatefromjpeg($ImgFile); break;
case 3 : $src = imagecreatefrompng($ImgFile); break;
case 6 : $src = imagecreatefrombmp($ImgFile); break;
default : $Message .= "Unsupported image type. You may upload only JPG, GIF, PNG or BMP images.\n\n";
endswitch;
if ($src === FALSE) $Message = "Unable to create image.";
// Calculate new dimensions from $MaxDim based on whether $SizeCentric is the Width or the Height
// However, do not stretch the image beyond its current size
if (strtolower($SizeCentric) === "height") :
if ($ImgHeight > $MaxDim) :
$newHeight = $MaxDim;
$newWidth = round( $ImgWidth * ($newHeight/$ImgHeight));
else :
$newHeight = $ImgHeight;
$newWidth = $ImgWidth;
endif;
else :
if ($ImgWidth > $MaxDim) :
$newWidth = $MaxDim;
$newHeight = round($ImgHeight * ($newWidth / $ImgWidth));
else :
$newHeight = $ImgHeight;
$newWidth = $ImgWidth;
endif;
endif;
$processimg = imagecreatetruecolor($newWidth, $newHeight);
// Maintain any GIF or PNG transparencies
if ($ImgType == 1 || $ImgType == 3) :
$transparent = imagecolorallocatealpha($processimg, 255, 255, 255, 127);
imagefill($processimg, 0, 0, $transparent);
imagealphablending($processimg, FALSE);
imagesavealpha($processimg, TRUE);
endif;
imagecopyresampled($processimg, $src, 0, 0, 0, 0,$newWidth, $newHeight, $ImgWidth, $ImgHeight);
// Convert image to JPG or PNG
ob_start();
switch ($ImgType) :
case 1 :
case 3 : imagepng($processimg, null); break;
default : imagejpeg($processimg, null, 100);
endswitch;
$image = ob_get_contents();
ob_end_clean();
imagedestroy($processimg);
endif;
if (!empty($Message)) :
$_SESSION['Message'] = $Message;
endif;
//(isset($image) && strlen($image) > 0) return $image;
return $image;
}
此非常基本的ShowImage()函数用于测试上述功能以及通过将图像流显示到屏幕尚未创建的其他功能:
// Used for diagnosing and testing image processing functions
function ShowImage($image) {
$mimetype = getimagesizefromstring($image);
// Add headers and output the image
Header("Pragma: no-cache");
Header('Content-type: ' . $mimetype['mime']);
echo $image;
}
在这里回答我自己的问题,但是根据@Phil和@Yoshi的评论,我意识到自己正在混合苹果和橙子。与其尝试对图像数据进行处理(如果正在使用的话),不如调用大多数函数需要文件路径。由于此时我实际上并未创建物理文件,因此我仅使用了临时文件,并且对每个@Yoshi ConvertImage()
函数仅进行了一次更改(将getimagesizefromstring()
更改为getimagesize()
),然后进行了其余操作对正在处理向函数提交表单的代码的修改: