将缩略图创建为文件非常容易,但我无法弄清楚如何使用PHP图像函数创建图像并将数据存储在变量中,我可以使用该变量将数据存储在数据库中表。
我是图像功能的新手,我不明白他们的工作。它们是以目录为导向的吗?因此,我必须为缩略图制作者创建类似tmp地图的内容,然后使用file_get_content然后删除图像。
或者我可以在这个过程中如何存储图像的数据。我不知道!
如果我需要将它们保存为文件,这就是我创建缩略图的方法:
//found at stackoverflow
function tumbmaker($updir, $img,$MaxWe=100,$MaxHe=150){
$arr_image_details = getimagesize($img);
$width = $arr_image_details[0];
$height = $arr_image_details[1];
$percent = 100;
if($width > $MaxWe) $percent = floor(($MaxWe * 100) / $width);
if(floor(($height * $percent)/100)>$MaxHe)
$percent = (($MaxHe * 100) / $height);
if($width > $height) {
$newWidth=$MaxWe;
$newHeight=round(($height*$percent)/100);
}else{
$newWidth=round(($width*$percent)/100);
$newHeight=$MaxHe;
}
if ($arr_image_details[2] == 1) {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
}
if ($arr_image_details[2] == 2) {
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
}
if ($arr_image_details[2] == 3) {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
}
if ($imgt) {
$old_image = $imgcreatefrom($img);
$new_image = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$imgt($new_image, $updir."_t.jpg");
return;
}
}
我用这段代码做了:
function tumbmaker($img_source,$MaxWe=100,$MaxHe=150){
$arr_image_details = getimagesize($img_source);
$width = $arr_image_details[0];
$height = $arr_image_details[1];
$percent = 100;
if($width > $MaxWe) $percent = floor(($MaxWe * 100) / $width);
if(floor(($height * $percent)/100)>$MaxHe)
$percent = (($MaxHe * 100) / $height);
if($width > $height) {
$newWidth=$MaxWe;
$newHeight=round(($height*$percent)/100);
}else{
$newWidth=round(($width*$percent)/100);
$newHeight=$MaxHe;
}
if ($arr_image_details[2] == 1) {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
}
if ($arr_image_details[2] == 2) {
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
}
if ($arr_image_details[2] == 3) {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
}
if ($imgt) {
$old_image = $imgcreatefrom($img_source);
$new_image = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$imgt($new_image, $updir."_t.jpg");
ob_start();
$imgt($new_image);
$imgData = ob_get_clean();
return $imgData;
}
}
阅读答案以获取更多信息
答案 0 :(得分:1)
如果没有为图像创建功能提供文件名,他们只需将图像数据写入PHP的输出缓冲区。
这意味着您可以截取图像数据:
ob_start();
$imgt($new_image);
$imgData = ob_get_clean();