codeigniter 无法在PHP中创建“webp image”的缩略图

4sup72z8  于 2023-03-27  发布在  PHP
关注(0)|答案(1)|浏览(109)

我正在上传图像和创建拇指,一切工作正常,但每当我试图上传“webp图像”然后图像不上传,我得到以下错误您的服务器不支持处理这种类型的图像所需的GD功能这里是我目前的代码,我错了吗?

$imagePrefix = time(); 
$imagename = "img_".$imagePrefix.trim($_FILES['file']['name']);
$config['upload_path'] = 'uploads/nft/'; 
$config['allowed_types'] = 'jpg|jpeg|png|gif|PNG|JPG|JPEG|avif|webp'; 
$config['file_name'] = $imagename; 
$this->load->library('upload',$config); 
if($this->upload->do_upload('file'))
    { 
        $uploadData = $this->upload->data(); 
        $filename = $uploadData['file_name']; 
        $datas=$_POST;
        $data = $this->upload->data();
        $this->load->library('image_lib');
        $config2['image_library'] = 'gd2';
        $config2['source_image'] = $uploadData['full_path'];
        $config2['new_image'] = './uploads/nft/thumb/'.$uploadData["raw_name"].$uploadData['file_ext'];
        $config2['create_thumb'] = TRUE;
        $config2['maintain_ratio'] = FALSE;
        $config2['width'] = 350;
        $config2['height'] = 350;
        $this->image_lib->initialize($config2);
$this->image_lib->clear();

}

hm2xizp9

hm2xizp91#

正常的文件上传方法用于调用另一个函数createThumbnail()调用

$config['remove_spaces'] = TRUE;
                $config['allowed_types'] = 'jpg|jpeg|gif|png';
                $config['upload_path'] = './images/files';
                $config['remove_spaces'] = TRUE;
                $config['encrypt_name'] = TRUE;
                $this->load->library('upload', $config);
                if ($this->upload->do_upload('img')) {
                    $imgDetailsd = $this->upload->data();
                    $this->createThumbnail($imgDetailsd['file_name'], 200, 200, "./images/site/files", "your thumbnail file location save");
                }

试试这个拇指函数创建你需要的图像尺寸

function createThumbnail($image_name,$new_width,$new_height,$uploadDir,$moveToDir)
{
$path = $uploadDir . '/' . $image_name;

$mime = getimagesize($path);

if($mime['mime']=='image/png') { 
    $src_img = imagecreatefrompng($path);
}
if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') {
    $src_img = imagecreatefromjpeg($path);
}   

$old_x          =   imageSX($src_img);
$old_y          =   imageSY($src_img);

$width = imagesx( $src_img );
$height = imagesy( $src_img );

if ($width > $height) {
    if($width < $new_width)
        $new_width = $width;
    
    else
    
    $new_width = $new_width;    
    
    
    $divisor = $width / $new_width;
    $new_height = floor( $height / $divisor);
}
else {
    
     if($height < $new_height)
         $new_height = $height;
     else
         $new_height =  $new_height;
     
    $divisor = $height / $new_height;
    $new_width = floor( $width / $divisor );
}

$dst_img        =   ImageCreateTrueColor($new_width,$new_height);

imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_width,$new_height,$old_x,$old_y); 

// New save location
$new_thumb_loc = $moveToDir . $image_name;

if($mime['mime']=='image/png') {
    $result = imagepng($dst_img,$new_thumb_loc,8);
}
if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') {
    $result = imagejpeg($dst_img,$new_thumb_loc,80);
}

imagedestroy($dst_img); 
imagedestroy($src_img);

return $result;

}

相关问题