我有这样的代码
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
}
]
型
我想生成缩略图图像的图像名称从数据库
1条答案
按热度按时间ui7jx7zq1#
这是你的代码的修改版本。在这段代码中,我添加了一个clear()调用,在每次迭代后重置image_lib库,并使用in_array()而不是多个if语句来检查支持的文件格式是否简化。
字符串