繁体   English   中英

如何调整图像大小并上传到具有不同大小的其他文件夹中?

[英]How to re-size image and upload in different folder with different size?

我是php新手。 对于我的购物网站项目,我需要您的帮助。

在我的add_product页面中,有产品图片选项。 当我提交表单时,文件图像将在3个不同的文件夹中重新调整大小,例如-

30x30 for Shopping cart icon in 'Product/Image/Icon' folder.
170x300 for Base image in 'Product/Image/Base' folder.
400x300 for Large View in 'Product/Image/Thumb' folder.

我也有一个数据库表-

ID
Product ID
thumb_name
thumb_type

提交表单后,每个文件也会生成一个唯一的新名称,并按以下方式保存在上表中:

ID : Auto increment
Product ID : 44545
thumb_name : new file name here
thumb_type:  Base Image

如何调整图像大小并上传到具有不同大小的不同文件夹中,并在MySQL数据库中插入文件名? 请任何人帮助我。

  1. 上传后,仅将图像基本名称存储到数据库中。 product1.jpg
  2. 然后在不同目录中创建具有相同名称的缩略图。

    上传的文件-uploads / product1.jpg
    小拇指- 上传/缩略图/small/product1.jpg

这是一个在php中创建拇指的函数

function createThumb($filepath, $thumbPath, $maxwidth, $maxheight, $quality=75)
{   
            $created=false;
            $file_name  = pathinfo($filepath);  
            $format = $file_name['extension'];
            // Get new dimensions
            $newW   = $maxwidth;
            $newH   = $maxheight;

            // Resample
            $thumb = imagecreatetruecolor($newW, $newH);
            $image = imagecreatefromstring(file_get_contents($filepath));
            list($width_orig, $height_orig) = getimagesize($filepath);
            imagecopyresampled($thumb, $image, 0, 0, 0, 0, $newW, $newH, $width_orig, $height_orig);

            // Output
            switch (strtolower($format)) {
                case 'png':
                imagepng($thumb, $thumbPath, 9);
                $created=true;
                break;

                case 'gif':
                imagegif($thumb, $thumbPath);
                $created=true;
                break;

                default:
                imagejpeg($thumb, $thumbPath, $quality);
                $created=true;
                break;
            }
            imagedestroy($image);
            imagedestroy($thumb);
            return $created;    
}

参数详细信息

$filepath // uploaded file path include name eg. uploads/product1.jpg
$thumbPath // thumbpath with name eg. uploads/thumbs/small/product1.jpg
$maxwidth // width for the thumb eg. 100
$maxheight// height for the thumb eg. 60
$quality=75 // quality for the thumb image 1 - 100 ...by default it is 75

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM