如何在php中上传jpeg图像
谁能告诉我,如何解决以下问题如何在php中上传jpeg图像,php,gd,Php,Gd,谁能告诉我,如何解决以下问题 Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /var/www/html/_controls/_images.php on line 28 Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/var/t
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /var/www/html/_controls/_images.php on line 28
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/var/tmp/phpaFbMJV' is not a valid JPEG file in /var/www/html/_controls/_images.php on line 28
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /var/www/html/_controls/_images.php on line 52
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /var/www/html/_controls/_images.php on line 28
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/var/tmp/phpaFbMJV' is not a valid JPEG file in /var/www/html/_controls/_images.php on line 28
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /var/www/html/_controls/_images.php on line 52
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /var/www/html/_controls/_images.php on line 28
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/var/tmp/phpUakeQQ' is not a valid JPEG file in /var/www/html/_controls/_images.php on line 28
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /var/www/html/_controls/_images.php on line 52
Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/_controls/_images.php:28) in /var/www/html/_templates/code/admin/edit_project_image.php on line 40
我的源代码:
phpaFbMJV显然不是JPEG文件?您提供的文件不是有效文件,请检查该文件。只有扩展名.jpg不一定是有效的jpg格式图像。一旦试图发现漏洞的用户很容易覆盖内容类型头,我建议您使用打开文件
接下来,您希望在保存图像时保留图像格式。我建议您只使用一种格式,例如png,这样您的所有图像现在都是相同的类型,您就不必再担心这些了。如果您显示代码检查是否安装了GD库,我们可以为您提供帮助,因为错误会有所不同,类似于警告:某些功能已被禁用..意味着我没有得到你,请你清楚地告诉我。但我上载了有效的jpeg图像文件。@ramkumar尝试使用move_uploaded_文件简单地移动图像,然后下载,以查看服务器接收到的内容。您的问题中缺少一些代码。$raw\u file\u post数组的内容是什么?
<?
class image_control
{
public $raw_image = "";
public $temp_loc = "";
public $image = "";
public $image_resized = "";
public $name = "";
public $type = "";
public $width = "";
public $height = "";
public $resize_to_width = "";
public $resize_to_height = "";
public $errors = array();
function __construct($raw_file_post){
$this->raw_image = $raw_file_post;
$this->temp_loc = $raw_file_post['tmp_name'];
$this->name = $raw_file_post['name'];
if($raw_file_post['type'] == "image/jpeg" || $raw_file_post['type'] == "image/pjpeg"){
$this->type = 'jpeg';
$this->image = imagecreatefromjpeg($this->temp_loc);
}
elseif($raw_file_post['type'] == "image/png" || $raw_file_post['type'] == "image/x-png"){
$this->type = 'png';
$this->image = imagecreatefrompng($this->temp_loc);
}
elseif($raw_file_post['type'] == "image/gif"){
$this->type = 'gif';
$this->image = imagecreatefromgif($this->temp_loc);
}
else
$error[] = 001;
list($this->width, $this->height) = getimagesize($this->temp_loc);
$this->image_resized = imagecreatetruecolor($this->width, $this->height);
imageantialias($this->image_resized, true);
imagealphablending($this->image_resized, false);
imagesavealpha($this->image_resized,true);
$transparent = imagecolorallocatealpha($this->image_resized, 255, 255, 255, 127);
imagefilledrectangle($this->image_resized, 0, 0, $this->width, $this->height, $transparent);
imagecopyresampled($this->image_resized, $this->image, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
$this->image = $this->image_resized;
$this->image_resized = "";
}
function resize($width, $height, $resample = false){
if($width !== '' && $width !== NULL && $width !== 'auto')
$this->resize_to_width = $width;
if($height !== '' && $height !== NULL && $height !== 'auto')
$this->resize_to_height = $height;
if($width == 'auto' || $height == 'auto'){
if($height == 'auto')
$this->resize_to_height = ($this->resize_to_width / $this->width) * $this->height;
if($width == 'auto')
$this->resize_to_width = ($this->resize_to_height / $this->height) * $this->width;
}
$this->image_resized = imagecreatetruecolor($this->resize_to_width, $this->resize_to_height);
imageantialias($this->image_resized, true);
imagealphablending($this->image_resized, false);
imagesavealpha($this->image_resized,true);
$transparent = imagecolorallocatealpha($this->image_resized, 255, 255, 255, 127);
imagefilledrectangle($this->image_resized, 0, 0, $this->resize_to_width, $this->resize_to_height, $transparent);
imagecopyresampled($this->image_resized, $this->image, 0, 0, 0, 0, $this->resize_to_width, $this->resize_to_height, $this->width, $this->height);
if($resample == true){
$this->image = $this->image_resized;
$this->width = $this->resize_to_width;
$this->height = $this->resize_to_height;
$this->image_resized = "";
$this->resize_to_width = "";
$this->resize_to_height = "";
}
}
function save($folder_dir, $name){
if(is_dir($folder_dir)){
if($this->type == 'jpeg')
imagejpeg($this->image, $folder_dir . '/' . $name . '.jpg', 100)
or die('Image error');
elseif($this->type == 'png')
imagepng($this->image, $folder_dir . '/' . $name . '.png')
or die('Image error');
elseif($this->type == 'gif')
imagegif($this->image, $folder_dir . '/' . $name . '.gif')
or die('Image error');
}
else
return false;
return true;
}
}
?>
/var/tmp/phpaFbMJV' is not a valid JPEG file
$this->image = @imagecreatefromstring(@file_get_contents($this->temp_loc));
if ($this->image === false) {
throw new Exception("File is unreachable or not a supported image");
}