如何在保存图像时创建缩略图

问题描述 投票:0回答:1

我有保存用户上传图像的代码。 是否可以同时创建缩略图?

function saveImages($dataArray, $idArticles, $folderPath, $prefixFile){
    $folderPathDefault = '../../media/articles/imm'.$idArticles.'/';
    $prefixFileDefault = 'photo_';
    if($folderPath == null){
        $folderPath = $folderPathDefault;
        array_map('unlink', array_filter((array) glob($folderPath."*")));
    }
    if($prefixFile == null){
        $prefixFile = $prefixFileDefault;
    }
    if (!is_dir($folderPath)) {
      // dir doesn't exist, make it
      mkdir($folderPath);
    }

    $count = 1;
    foreach ($dataArray as $data) {
        $image_parts = explode(";base64,", $data);
        $image_type_aux = explode("image/", $image_parts[0]);
        $image_type = $image_type_aux[1];
        $image_base64 = base64_decode($image_parts[1]);
        $file = $folderPath . $prefixFile . $count . '.jpg';
        file_put_contents($file, $image_base64);
        $count ++;
    }

    
}

谢谢你

php image resize thumbnails onload
1个回答
0
投票

对于那些对此功能感兴趣或已解决的人:

function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  
    return $dst;
  }


function saveImages($dataArray, $idArticles, $folderPath, $prefixFile){
    $folderPathDefault = '../../media/articles/imm'.$idArticles.'/';
    $prefixFileDefault = 'photo_';
    if($folderPath == null){
        $folderPath = $folderPathDefault;
        array_map('unlink', array_filter((array) glob($folderPath."*")));
    }
    if($prefixFile == null){
        $prefixFile = $prefixFileDefault;
    }
    if (!is_dir($folderPath)) {
      // dir doesn't exist, make it
      mkdir($folderPath);
    }

    $count = 1;
    foreach ($dataArray as $data) {
        $image_parts = explode(";base64,", $data);
        $image_type_aux = explode("image/", $image_parts[0]);
        $image_type = $image_type_aux[1];
        $image_base64 = base64_decode($image_parts[1]);
        $file = $folderPath . $prefixFile . $count . '.jpg';
        file_put_contents($file, $image_base64);
        $count ++;
    }

    if (!file_exists('../../media/articles/imm'.$idArticles.'/thumbnail/')) {
        mkdir('../../media/articles/imm'.$idArticles.'/thumbnail/', 0777, true);
        }
        $dir = "../../media/articles/imm".$idArticles."/thumbnail/";
        array_map('unlink', glob("{$dir}*.jpg"));

    $immagine= "../../media/articles/imm".$idArticles."/";
    $files = glob("$immagine*.*");

    $uno= 1;
    for ($i=0; $i<count($files); $i++) {
        $image = $files[$i];
        $url = $image;
        
        $imgResize = resize_image($url, 360, 270);
        
        $path = '../../media/articles/imm'.$idArticles.'/thumbnail/thumbnail_photo_'.$uno++.'.jpg';
        imagejpeg($imgResize, $path);

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.