Exemple #1
0
 /**
  * From: https://stackoverflow.com/questions/4590441/php-thumbnail-image-resizing-with-proportions
  */
 function resize(&$img, $size)
 {
     $pm = new PictshareModel();
     $sd = $pm->sizeStringToWidthHeight($size);
     $maxwidth = $sd['width'];
     $maxheight = $sd['height'];
     $width = imagesx($img);
     $height = imagesy($img);
     if (!ALLOW_BLOATING) {
         if ($maxwidth > $width) {
             $maxwidth = $width;
         }
         if ($maxheight > $height) {
             $maxheight = $height;
         }
     }
     if ($height > $width) {
         $ratio = $maxheight / $height;
         $newheight = $maxheight;
         $newwidth = $width * $ratio;
     } else {
         $ratio = $maxwidth / $width;
         $newwidth = $maxwidth;
         $newheight = $height * $ratio;
     }
     $newimg = imagecreatetruecolor($newwidth, $newheight);
     $palsize = ImageColorsTotal($img);
     for ($i = 0; $i < $palsize; $i++) {
         $colors = ImageColorsForIndex($img, $i);
         ImageColorAllocate($newimg, $colors['red'], $colors['green'], $colors['blue']);
     }
     imagefill($newimg, 0, 0, IMG_COLOR_TRANSPARENT);
     imagesavealpha($newimg, true);
     imagealphablending($newimg, true);
     imagecopyresampled($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
     $img = $newimg;
 }