php - 输出空白图像的图像转换和缩放功能
问题描述
构建一个 ConvertImage() 函数,该函数取自我多年来编写的、我的站点已经使用的部分代码,它应该转换(如果是 BMP 或 GIF)并调整作为数据字符串提供给它的图像的大小。它正在工作,并且图像以适当的大小返回,但为空白。显然我错过了一些重要的事情,但是什么?
请注意,这不会将图像上传到任何地方,也不会将其插入数据库列,至少目前不是。它的目的只是调整大小和转换它,不幸的是 ImageMagick 尚不可用。
为了测试,一个简单的表单将图像从文件系统提交到this,它也调用了函数(参数详见函数中的备注):
<?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 的评论,我意识到我在混合苹果和橙子。与其尝试对正在使用的图像数据进行处理,不如说大部分被调用的函数需要文件路径。由于此时我实际上并没有创建物理文件,我只是使用临时文件并对ConvertImage()
每个 @Yoshi 的函数进行了一次更改(更改getimagesizefromstring()
为getimagesize()
),然后对正在处理的代码进行了其余的修改表单提交给函数。@Phil 已经对原始问题进行了更改。
<?php include_once("/var/www/html/site.loc/functions.php");
if (isset($_FILES['fileToProcess'])) :
$image = $_FILES['fileToProcess']['tmp_name'];
$image = ConvertImage($image, 250, "Width", "");
ShowImage($image);
endif;
?>
推荐阅读
- tfs - TFS 2017 如何删除构建?
- xamarin - 如何在 Xamarin 中使用 RotateDrawable?
- c# - 我不能打开对话框最重要?
- excel - 如何将单元格更改为货币
- jquery - 如何调整我从演示站点获取的这个轮播的大小?
- java - java.io.ioexception:读取失败,套接字可能关闭或超时 Java Android Studio
- google-cloud-dataflow - 用于流式传输的 Google Cloud Dataflow,dataflow 何时会自动关闭并清理 VM 实例?
- javascript - 如何在 Kendo 事件处理程序中同时传递事件对象和自定义参数?
- hyperledger-fabric - 链码实例化错误,日志显示 npm err!缺少脚本:启动,即使存在带有启动脚本的 package.json
- mongodb - 以秒为单位查找日期范围重叠的长度