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

wdebmtf2  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(133)

我有这样的代码

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
  }
]


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

ui7jx7zq

ui7jx7zq1#

这是你的代码的修改版本。在这段代码中,我添加了一个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();
    }
}

字符串

相关问题