我想在资产文件夹中创建缩略图,但它只创建一个文件

I want to create thumbnail image in asset folder but it only create one file

提问人:twodollaryus 提问时间:11/7/2023 最后编辑:user1191247twodollaryus 更新时间:11/7/2023 访问量:28

问:

我有这样的代码

function generate_thumb(){ 
    $data['record'] = $this->model_produk->gambar_produk();

    foreach ($data['record']->result_array() as $key => $ss) {

        $thumbnailConfig = array();
        $imagePath = 'asset/img_produk/' . $ss['image_produk'];

        // Use pathinfo to get the file extension
        $imageInfo = pathinfo($imagePath);
        $fileExtension = strtolower($imageInfo['extension']);

        if ($fileExtension === 'jpg' || $fileExtension === 'jpeg') {
            $thumbnailConfig['image_library'] = 'gd2';
        } elseif ($fileExtension === 'png') {
            $thumbnailConfig['image_library'] = 'gd2'; // You can use 'imagemagick' for PNG if available
        } else {
            // Handle unsupported image formats or skip them
            continue;
        }

        $thumbnailConfig['source_image'] = $imagePath;
        $thumbnailConfig['new_image'] = 'asset/img_produk/thumb/';
        $thumbnailConfig['create_thumb'] = true;
        $thumbnailConfig['maintain_ratio'] = TRUE;

        $img = getimagesize($imagePath);

        // Calculate the thumbnail dimensions
        $new_width = $img[0] * 0.4;
        $new_height = $img[1] * 0.4;

        $thumbnailConfig['width'] = $new_width;
        $thumbnailConfig['height'] = $new_height;

        $this->load->library('image_lib', $thumbnailConfig);
        if (!$this->image_lib->resize()) {
            // Handle any errors or log them
            echo $this->image_lib->display_errors();
        }
    }
}

但它只在结果上生成一个文件(12_1689302754090.png),有人可以解释错误吗

像这样$data['record']的结果

[
  {
    "id": "62",
    "image_produk": "12_1689302754090.png",
    "id_produk": "12",
    "id_warna": "23",
    "nama_produk": "Ladies Regular Crewneck Bear",
    "artikel": "zza",
    "id_image": "62",
    "warna": null
  },
  {
    "id": "63",
    "image_produk": "12_1689302754264.png",
    "id_produk": "12",
    "id_warna": "23",
    "nama_produk": "Ladies Regular Crewneck Bear",
    "artikel": "zza",
    "id_image": "63",
    "warna": null
  },
  {
    "id": "64",
    "image_produk": "12_1689302754451.png",
    "id_produk": "12",
    "id_warna": "23",
    "nama_produk": "Ladies Regular Crewneck Bear",
    "artikel": "zza",
    "id_image": "64",
    "warna": null
  }
]

我想从数据库中生成缩略图图像名称

php codeigniter

评论


答:

0赞 Jenny 11/7/2023 #1

下面是代码的修订版本。在此代码中,我添加了一个 clear() 调用,用于在每次迭代后重置 image_lib 库,并使用 in_array() 而不是多个 if 语句检查支持的文件格式是否要简化。

function generate_thumb() {
    $data['record'] = $this->model_produk->gambar_produk();

    foreach ($data['record']->result_array() as $key => $ss) {
        $imagePath = 'asset/img_produk/' . $ss['image_produk'];

        // Use pathinfo to get the file extension
        $imageInfo = pathinfo($imagePath);
        $fileExtension = strtolower($imageInfo['extension']);

        if (!in_array($fileExtension, ['jpg', 'jpeg', 'png'])) {
            // Handle unsupported image formats or skip them
            echo "Unsupported format for image: " . $ss['image_produk'] . "\n";
            continue;
        }

        $thumbnailConfig = [
            'image_library'  => 'gd2',
            'source_image'   => $imagePath,
            'new_image'      => 'asset/img_produk/thumb/',
            'create_thumb'   => true,
            'maintain_ratio' => true,
        ];

        $img = getimagesize($imagePath);

        // Calculate the thumbnail dimensions
        $thumbnailConfig['width'] = $img[0] * 0.4;
        $thumbnailConfig['height'] = $img[1] * 0.4;

        $this->load->library('image_lib', $thumbnailConfig);

        if (!$this->image_lib->resize()) {
            // Handle any errors or log them
            echo "Error resizing image: " . $ss['image_produk'] . "\n";
            echo $this->image_lib->display_errors();
        } else {
            echo "Thumbnail created for image: " . $ss['image_produk'] . "\n";
        }

        // Clear the image_lib settings to prepare for next image
        $this->image_lib->clear();
    }
}