gpt4 book ai didi

php - 下载时 Mysql blob 图像没有尺寸

转载 作者:太空宇宙 更新时间:2023-11-03 11:08:37 26 4
gpt4 key购买 nike

出于安全原因,我在 MYSQL 中存储了一些图像。当我下载图像时,文件会以正确的文件大小和名称下载,但没有显示图像。当我查看它的属性时,图像也没有尺寸。我正在使用 cakephp。

header("Content-type: ".$file['UploadFile']['file_extension']);
header("Content-Disposition: attachment; filename=\"".$file['UploadFile']['file_name']."\"");
header("Content-length: ".$file['UploadFile']['file_size']);



echo $file['UploadFile']['file_content'];

我用下面的代码保存我的图像......

public function image_upload($fileName, $source, $extension, $file_size) {
$this->loadModel('UploadFile');

$content = addslashes(file_get_contents($source));

$file_data = array('UploadFile' => array('title' => $fileName, 'file_content' => $content, 'file_name' => $fileName, 'file_extension' => $extension, 'file_size' => $file_size, 'file_type' => 'image-art'));

$this->UploadFile->create();
$this->UploadFile->save($file_data);

$file_id = $this->UploadFile->id;
return $file_id;
}

最佳答案

你可能有这样的可能原因很少

一个。错误的内容类型.. 你有 Content-type: ".$file['UploadFile']['file_extension'] .. 这在你的代码中是 worng`

例如。文件扩展名格式类似“.jpg”,内容类型为image/jpeg

B.您的文件可能未正确保存,这一定会导致此类图像损坏

C.文件必须在上传过程中被截断

D. $content = addslashes(file_get_contents($source)); .. 永远不要 addslashes 到图像 .. 它会搞砸或准备好 stripslashes 当你加载图像时

要在下载之前知道您的图像是否有效,您可以运行此代码

$image = ImageCreateFromString($file['UploadFile']['file_content']);
if(!$image)
Something is Wrong

您还可以使用 getimagesize 添加额外的验证

编辑 1

概念验证

$image = ImageCreateFromString ( $file ['UploadFile'] ['file_content'] );
if ($image) {

/**
* Check If Height and Width is grater than 1
*/
if (ImageSX ( $image ) > 1 && ImageSY ( $image ) > 1) {
$extention = strtolower ( $file ['UploadFile'] ['file_extension'] );
switch ($extention) {
case "png" :
header ( 'Content-Type: image/png' ); // This is just example
imagepng ( $image );
break;

case "gif" :
header ( 'Content-Type: image/gif' ); // This is just example
imagegif ( $image );
break;

case "jpg" :
case "jpeg" :
header ( 'Content-Type: image/jpg' ); // This is just example
imagejpeg ( $image );
break;

default :
die ( "Unsupported Image" );
break;

}

} else {
die ( "Fake Image" );
}
} else {
die ( "Invalid Image -- Contains Errors" );
}}

关于php - 下载时 Mysql blob 图像没有尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10096465/

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