gpt4 book ai didi

php - 创建图像的缩略图,然后将其保存到数据库中

转载 作者:行者123 更新时间:2023-11-29 14:25:01 25 4
gpt4 key购买 nike

我正在创建图像的缩略图,然后将其保存到数据库中,但我不知道该怎么做。这是我的代码

 function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) 
{
$pathToImages = "../uploads/images/";
// open the directory
$dir = opendir( $pathToImages );

// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension

$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{

$pathToThumbs = "../uploads/images/thumbs/";
//echo "Creating thumbnail for {$fname} <br />";

// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );

// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width,$new_height, $width, $height );

// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
$thumbimage = $fname;
}
}
// close the directory
closedir( $dir );

}
createThumbs("/uploads/documents","/uploads/images/thumbs/",100);

创建缩略图很好,但现在我不知道如何将其保存在数据库中(缺乏技能)

最佳答案

为什么不将结果保存在文件中?

我认为你必须在数据库字段中选择文本类型,然后将图像内容保存在那里,

并读取带有“Content-type: image/jpg” header 的不同 php 文件中的数据。

例如:

$query = "SELECT image FROM youtablename WHERE id = 10";
$result = mysql_query($query) or die('Error, query failed');
$content=mysql_result($result,0,"image");
header("Content-type: image/jpg");
echo $content;

关于php - 创建图像的缩略图,然后将其保存到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11152171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com