public function resize($id, $width = NULL, $height = NULL)
 {
     $id = intval($id);
     if (empty($id)) {
         return $this->error_param()->setStatusCode(404);
     }
     $attachment = $this->model->get($id);
     if (empty($attachment)) {
         return $this->failure('attachment::attachment.failure_noexists')->setStatusCode(404);
     }
     if ($attachment->file_type() != 'image') {
         return $this->failure('attachment::attachment.failure_resize');
     }
     //获取远程文件
     $attachment->sync();
     $full_path = $attachment->full_path();
     $img = Image::make($full_path);
     if (!empty($width) && $img->width() > $width || !empty($height) && $img->height() > $height) {
         $wh = aspect_ratio($img->width(), $img->height(), $width, $height);
         extract($wh);
         $new_path = storage_path(str_replace('.', '[dot]', $attachment->relative_path()) . ';' . $width . 'x' . $height . '.' . $attachment->ext);
         if (!file_exists($new_path)) {
             !is_dir($path = dirname($new_path)) && mkdir($path, 0777, TRUE);
             $img->resize($width, $height, function ($constraint) {
                 $constraint->aspectRatio();
             })->save($new_path);
         }
     } else {
         $new_path = $full_path;
     }
     unset($img);
     $mime_type = Mimes::getInstance()->mime_by_ext($attachment->ext);
     $content_length = NULL;
     //$attachment->size;
     $last_modified = true;
     $etag = true;
     $cache = TRUE;
     return response()->preview($new_path, [], compact('mime_type', 'etag', 'last_modified', 'content_length', 'cache'));
 }
Exemple #2
0
$i = 0;
// maps counter
$j = 0;
// array position counter
//global fields
$title = get_sub_field('maps_set_title');
$subtitle = get_sub_field('maps_set_subtitle');
$description = get_sub_field('maps_general_description');
$download = get_sub_field('download');
//propieties - aspect ratio
while (has_sub_field('maps')) {
    ++$i;
    $image = get_sub_field('map');
    $images_url[] = $image['url'];
    $aspect_ratio[] = aspect_ratio($images_url[$j]);
    ++$j;
}
$aspect_ratio = array_icount_values($aspect_ratio);
if ($aspect_ratio['portrait'] > $aspect_ratio['landscape']) {
    $aspect_ratio = "portrait";
} else {
    $aspect_ratio = "landscape";
}
//propieties - description
if ($description == "") {
    $has_description = "no_description";
} else {
    $has_description = "with_description";
}
//propieties builder
Exemple #3
0
 private function resize($img_name, $bildenavn, $picsize, $max_height, $dir, $thumb = false)
 {
     $bildetekst = $this->_WatermarkText;
     $fontsize = ceil($picsize / 17);
     $fontfile = $this->_WatermarkFont;
     $imgInfo = getimagesize($img_name);
     $imgType = $imgInfo[2];
     switch ($imgType) {
         case 2:
             $src_img = @imagecreatefromjpeg($img_name);
             break;
         case 3:
             $src_img = @imagecreatefrompng($img_name);
             break;
         case 1:
             $src_img = @imagecreatefromgif($img_name);
             break;
         default:
             return false;
     }
     if (!$src_img) {
         return false;
     }
     //NewWidth = GivenHeight * (OriginalWidth / OriginalHeight)
     //NewHeight = GivenWidth * (OriginalHeight / OriginalWidth)
     list($new_w, $new_h, $type, $attr) = $imgInfo;
     $new_width = $picsize;
     if ($new_h > $new_w && $new_w < $picsize) {
         $new_width = $new_w;
     }
     $new_h = aspect_ratio($new_w, $new_h, $new_width);
     if ($new_h > 250) {
         $new_h = aspect_ratio($new_w, $new_h, $new_width);
         $new_h = 250;
     }
     if ($thumb && $new_h > $picsize * 2) {
         $new_h = aspect_ratio($new_w, $new_h, $new_width);
     }
     if ($new_w > $picsize) {
         $new_w = $picsize;
     }
     /*	if ($new_w > $new_h) {
     			// landscape image
     			
     			$org_h = $new_h;
     			
     			$new_h = ($new_w * $new_h / $new_w) / 2;
     			$new_w = $picsize;
     			
     			if ($new_h < 50 && $org_h < $max_height) {
     				$new_h = $org_h;
     			}
     			
     			if ($new_h > $max_height) {
     				$new_h = $picsize;
     				$new_w = ($max_height * $new_w / $new_h);
     			}
     		} elseif ($new_h > $new_w) {
     			// portrait image
     			
     			$new_h = $max_height;
     			$new_w = (int)($max_height * $new_w / $new_h);
     			
     			if ($new_w > $picsize) {
     				$new_w = $picsize;
     				$new_h = ($picsize * $new_h / $new_w);
     			}
     		} else {
     			// square image
     			$new_w = $picsize;
     			$new_h = $max_height;
     		}
     		
     		if ($new_w == 0) {
     			$new_w = $picsize;
     			
     			if ($new_w > $imgInfo[0]) {
     				$new_w = $imgInfo[0];
     			}
     		}*/
     if (!$thumb) {
         if ($this->_MaxImageWidth > $imgInfo[0] && $this->_MaxImageHeight > $imgInfo[1]) {
             $new_w = $imgInfo[0];
             $new_h = $imgInfo[1];
         }
     }
     /*if ($new_h > $imgInfo[1]) {
     			$new_h = $imgInfo[1];
     		}*/
     $new_img = imagecreatetruecolor($new_w, $new_h);
     if (imagecopyresampled($new_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img)) == false) {
         return false;
     }
     if ($this->_AddLogo == true && $thumb == false) {
         $logoinfo = getimagesize($this->_LogoSRC);
         if ($logoinfo[0] <= $new_w + 70 && $logoinfo[1] <= $new_h + 50) {
             switch ($logoinfo[2]) {
                 case 2:
                     $logo_src = @imagecreatefromjpeg($this->_LogoSRC);
                     break;
                 case 3:
                     $logo_src = imagecreatefrompng($this->_LogoSRC);
                     break;
                 case 1:
                     $logo_src = @imagecreatefromgif($this->_LogoSRC);
                     break;
                 default:
                     return false;
                     break;
             }
             if (!$logo_src) {
                 return false;
             }
             $dst_x = $new_w - $logoinfo[0] - 5;
             $dst_y = $new_h - $logoinfo[1] - 5;
             imagecopymerge($new_img, $logo_src, $dst_x, $dst_y, 0, 0, $logoinfo[0], $logoinfo[1], 100);
         }
     } elseif ($this->_Watermark == true && $thumb == false) {
         $colors = $this->rgb();
         $textcolor = imagecolorallocate($new_img, $colors[0], $colors[1], $colors[2]);
         //Farge på teksten
         $textinfo = imagettfbbox($fontsize, 0, $fontfile, $bildetekst);
         $stringwidth = $textinfo[2] - $textinfo[0];
         $y = $new_h - 5;
         $x = $new_w - $stringwidth - 5;
         imagettftext($new_img, $fontsize, 0, $x, $y, $textcolor, $fontfile, $bildetekst);
     }
     $savepos = $dir . $bildenavn;
     switch ($imgType) {
         case 2:
             if ($this->_JPGQuality > 100) {
                 $this->_JPGQuality = 100;
             }
             if ($this->_JPGQuality < 0) {
                 $this->_JPGQuality = 75;
             }
             imageinterlace($new_img, 1);
             imagejpeg($new_img, $savepos, $this->_JPGQuality);
             break;
         case 3:
             #imageantialias ($new_img, true);
             imagepng($new_img, $savepos);
             break;
         case 1:
             #imageantialias ($new_img, true);
             imagepng($new_img, $savepos);
             //imagegif ($new_img, $savepos);
             break;
         default:
             return false;
             break;
     }
     imagedestroy($new_img);
     imagedestroy($src_img);
     return true;
 }