Home > Back-end >  how to create a thumbnail while saving the image
how to create a thumbnail while saving the image

Time:10-20

i have this code where the user uploaded image is saved. is it possible in the meantime to create a thumbnail?

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

    
}

Thank you

CodePudding user response:

For those interested or solved with this function:

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);

    }
}
  • Related