public function pullImage($source) { $this->deleteImage(); $ex = FileHelper::getEx($source); $path = $this->path_to_source = static::getTmpPath('source', $ex); $absPath = static::getAbsPath($path); copy($source, $absPath); foreach (['big', 'mid'] as $type) { $path = $this->{"path_to_{$type}"} = static::getTmpPath($type, $ex); $absPath = static::getAbsPath($path); static::resizeImage($type, $source, $absPath); } $this->evalHash(); }
public static function resize($params) { $image_name = basename($params['source']); $image_ex = FileHelper::getEx($params['source']); $image_ex = strtolower($image_ex); $sizes = getimagesize($params['source']); $oldImage = self::from_file($params['source']); $crop = isset($params['crop']) ? $params['crop'] : false; if ($image_ex and $sizes and $oldImage) { list($oldWidth, $oldHeight) = $sizes; if ($params['width'] != "auto" and $params['height'] != "auto") { if ($crop) { $widthScale = $params['width'] / $oldWidth; $heightScale = $params['height'] / $oldHeight; $scale = min($widthScale, $heightScale); $transWidth = $params['width'] / $scale; $transHeight = $params['height'] / $scale; $dst_x = 0; $dst_y = 0; $src_x = $oldWidth / 2 - $transWidth / 2; $src_y = $oldHeight / 2 - $transHeight / 2; $dst_w = $params['width']; $dst_h = $params['height']; $src_w = $transWidth; $src_h = $transHeight; } else { $widthScale = $params['width'] / $oldWidth; $heightScale = $params['height'] / $oldHeight; $scale = min($widthScale, $heightScale); $width = $oldWidth * $scale; $height = $oldHeight * $scale; $dst_x = $params['width'] / 2 - $width / 2; $dst_y = $params['height'] / 2 - $height / 2; $src_x = 0; $src_y = 0; $dst_w = $width; $dst_h = $height; $src_w = $oldWidth; $src_h = $oldHeight; } } else { if ($params['height'] == "auto") { $scale = $params['width'] / $oldWidth; $params['height'] = $oldHeight * $scale; } else { $scale = $params['height'] / $oldHeight; $params['width'] = $oldWidth * $scale; } $dst_x = 0; $dst_y = 0; $src_x = 0; $src_y = 0; $dst_w = $params['width']; $dst_h = $params['height']; $src_w = $oldWidth; $src_h = $oldHeight; } $imageNew = imagecreatetruecolor($params['width'], $params['height']); if ($image_ex == "png") { imagealphablending($imageNew, false); imagesavealpha($imageNew, true); $white = imagecolorallocatealpha($imageNew, 255, 255, 255, 127); imagefilledrectangle($imageNew, 0, 0, $params['width'], $params['height'], $white); } else { $white = imagecolorallocate($imageNew, 255, 255, 255); imagefilledrectangle($imageNew, 0, 0, $params['width'], $params['height'], $white); } imagecopyresampled($imageNew, $oldImage, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); switch ($image_ex) { case "jpg": case "jpeg": $q = isset($params['quality']) ? $params['quality'] : 100; imagejpeg($imageNew, $params['dest'], $q); break; case "png": imagepng($imageNew, $params['dest']); break; case "gif": imagegif($imageNew, $params['dest']); break; } } }