/** * Get a resized image URL for an attachment image * * Uses Google Cloud Storage to resize and serve an attachment image. * * @wp-filter image_downsize * * @param null|array $data Existing data (we always override) * @param int $id Attachment ID * @param string $size Size ID * @return array Indexed array of URL, width, height, is intermediate */ public static function get_intermediate_url($data, $id, $size) { $file = get_attached_file($id); if (0 !== strpos($file, 'gs://') || self::$skip_image_filters) { return $data; } $sizes = self::image_sizes(); if (is_array($size)) { $size = ['width' => $size[0], 'height' => $size[1], 'crop' => false]; } else { $size = $sizes[$size]; } $options = []; // If height or width is null (i.e. full size), $real_size will be // null, providing us a way to tell if the size is intermediate $real_size = max($size['height'], $size['width']); if ($real_size) { $options = ['size' => $real_size, 'crop' => (bool) $size['crop']]; } else { $options = ['size' => 0, 'crop' => false]; } $baseurl = get_post_meta($id, '_appengine_imageurl', true); $cached_file = get_post_meta($id, '_appengine_imageurl_file', true); $secure_urls = (bool) get_option(self::USE_SECURE_URLS_OPTION, false); if (empty($baseurl) || $cached_file !== $file) { try { if (self::is_production()) { $options = ['secure_url' => $secure_urls]; $baseurl = CloudStorageTools::getImageServingUrl($file, $options); } else { $baseurl = CloudStorageTools::getPublicUrl($file, $secure_urls); } update_post_meta($id, '_appengine_imageurl', $baseurl); update_post_meta($id, '_appengine_imageurl_file', $file); } catch (CloudStorageException $e) { syslog(LOG_ERR, 'There was an exception creating the Image Serving URL, details ' . $e->getMessage()); self::$skip_image_filters = true; $data = image_downsize($id, $size); self::$skip_image_filters = false; return $data; } } $url = $baseurl; // Only append image options to the URL if we're running in production, // since in the development context getPublicUrl() is currently used to // generate the URL. if (self::is_production()) { if (!is_null($options['size'])) { $url .= '=s' . $options['size']; if ($options['crop']) { $url .= '-c'; } } else { $url .= '=s0'; } } $data = [$url, $size['width'], $size['height'], (bool) $real_size]; return $data; }
private function executeGetImageUrlErrorTest($error_code, $expected_message) { $this->expectFilenameTranslation('/gs/mybucket/photo.jpg', 'some_blob_key'); $req = new \google\appengine\ImagesGetUrlBaseRequest(); $resp = new \google\appengine\ImagesGetUrlBaseResponse(); $req->setBlobKey('some_blob_key'); $req->setCreateSecureUrl(false); $exception = new \google\appengine\runtime\ApplicationError($error_code, 'a message'); $this->setExpectedException('\\google\\appengine\\api\\cloud_storage\\CloudStorageException', $expected_message); $this->apiProxyMock->expectCall('images', 'GetUrlBase', $req, $exception); CloudStorageTools::getImageServingUrl('gs://mybucket/photo.jpg'); $this->apiProxyMock->verify(); }
public function testGetImageUrlUsingDefaultKeyword() { $gs_filename = 'gs://#default#/photo.jpg'; $this->expectGetDefaultBucketName('bucket'); $this->expectApcFetch('__DEFAULT_GCS_BUCKET_NAME__', false, false); $this->expectApcStore('__DEFAULT_GCS_BUCKET_NAME__', 'bucket', true); $this->expectFilenameTranslation('/gs/bucket/photo.jpg', 'some_blob_key'); $this->expectImageGetUrlBase('some_blob_key', false, 'http://magic-url'); $url = CloudStorageTools::getImageServingUrl($gs_filename); $this->assertEquals('http://magic-url', $url); $this->apiProxyMock->verify(); }
public function image_path() { return CloudStorageTools::getImageServingUrl('gs://qiang2015/' . $this->filename, ['size' => 400, 'crop' => true]); }
protected function upload_file($tmp, $name) { //mydie($this->_media_dir,$this->_media); $ext = @strtolower(end(explode('.', $name))); $user = Bootstrap::$main->user; if (isset($this->data['flowIdentifier'])) { $lp = $this->data['flowIdentifier']; } else { $lp = 1 + $this->image()->getUsersCount($user['id']); } $name = $this->_prefix . '/' . $user['md5hash'] . '/' . md5($lp . '-' . $name) . '.' . $ext; $chunks = false; $original_name = $name; if (isset($this->data['flowTotalChunks']) && $this->data['flowTotalChunks'] > 1 && isset($this->data['flowChunkNumber'])) { $chunks = true; $name .= '.part' . $this->data['flowChunkNumber']; } if ($this->_appengine) { $file = 'gs://' . CloudStorageTools::getDefaultGoogleStorageBucketName() . '/' . $name; move_uploaded_file($tmp, $file); } else { $file = $this->_media_dir . '/' . $name; @mkdir(dirname($file), 0755, true); move_uploaded_file($tmp, $file); //mydie(exif_read_data($tmp)); } if ($chunks) { if (!$this->checkChunks($file)) { return false; } $name = $original_name; $file = preg_replace('/\\.part[0-9]+$/', '', $file); } if (!file_exists($file) || !filesize($file)) { $this->error(18); } $model = new imageModel(); $model->user = $user['id']; $model->src = $name; $model->d_uploaded = Bootstrap::$main->now; $exif = []; $imagesize = @getimagesize($file, $exif); if (!is_array($imagesize) || !$imagesize[0]) { $imagesize = [5000, 5000]; } if (is_array($exif)) { foreach ($exif as $k => $a) { if (substr($a, 0, 4) == 'Exif') { $matches = []; preg_match_all('/[0-9]{4}:[0-9]{2}:[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $a, $matches); $d = ''; if (isset($matches[0][1])) { $d = $matches[0][1]; } elseif (isset($matches[0][0])) { $d = $matches[0][0]; } if ($d) { $d = preg_replace('/([0-9]{4}):([0-9]{2}):([0-9]{2})/', '\\1-\\2-\\3', $d); $model->d_taken = $this->strtotime($d); } } } } if ($this->_appengine) { $model->url = CloudStorageTools::getImageServingUrl($file, ['size' => 0 + Bootstrap::$main->getConfig('image_size'), 'secure_url' => Bootstrap::$main->getConfig('protocol') == 'https']); $full = CloudStorageTools::getImageServingUrl($file, ['size' => 1234, 'secure_url' => Bootstrap::$main->getConfig('protocol') == 'https']); $model->full = str_replace('=s1234', '=s' . Bootstrap::$main->getConfig('full_size'), $full); $model->thumbnail = CloudStorageTools::getImageServingUrl($file, ['size' => 0 + Bootstrap::$main->getConfig('thumbnail_size'), 'secure_url' => Bootstrap::$main->getConfig('protocol') == 'https']); $model->square = CloudStorageTools::getImageServingUrl($file, ['size' => 0 + Bootstrap::$main->getConfig('square_size'), 'secure_url' => Bootstrap::$main->getConfig('protocol') == 'https', 'crop' => true]); } else { $image = new Image($file); $w = $h = 0; if ($imagesize[0] > Bootstrap::$main->getConfig('image_size')) { $w = Bootstrap::$main->getConfig('image_size'); $img = preg_replace("/\\.{$ext}\$/", '-i.' . $ext, $file); $image->min($img, $w, $h, true); $model->url = 'http://' . $_SERVER['HTTP_HOST'] . $this->_media . '/' . preg_replace("/\\.{$ext}\$/", '-i.' . $ext, $name); } else { $model->url = 'http://' . $_SERVER['HTTP_HOST'] . $this->_media . '/' . $name; } $w = $h = 0; if ($imagesize[0] > Bootstrap::$main->getConfig('full_size')) { $w = Bootstrap::$main->getConfig('full_size'); $img = preg_replace("/\\.{$ext}\$/", '-f.' . $ext, $file); $image->min($img, $w, $h, true); $model->full = 'http://' . $_SERVER['HTTP_HOST'] . $this->_media . '/' . preg_replace("/\\.{$ext}\$/", '-f.' . $ext, $name); } else { $model->full = 'http://' . $_SERVER['HTTP_HOST'] . $this->_media . '/' . $name; } $w = $h = 0; if ($image->w() > $image->h()) { $w = Bootstrap::$main->getConfig('thumbnail_size'); } else { $h = Bootstrap::$main->getConfig('thumbnail_size'); } $thmb = preg_replace("/\\.{$ext}\$/", '-t.' . $ext, $file); $image->min($thmb, $w, $h, true); $model->thumbnail = 'http://' . $_SERVER['HTTP_HOST'] . $this->_media . '/' . preg_replace("/\\.{$ext}\$/", '-t.' . $ext, $name); $w = $h = Bootstrap::$main->getConfig('square_size'); $square = preg_replace("/\\.{$ext}\$/", '-s.' . $ext, $file); $image->min($square, $w, $h, false, true); $model->square = 'http://' . $_SERVER['HTTP_HOST'] . $this->_media . '/' . preg_replace("/\\.{$ext}\$/", '-s.' . $ext, $name); } $model->save(); $ret = $model->data(); if ($ctx = Bootstrap::$main->session('image_ctx')) { $model->setLabels($ctx); $ret['labels'] = $model->getLabels(); if (is_array($ctx)) { foreach ($ctx as $k => $e) { if ($k == 'event') { $event = new eventModel($e); if ($event->user == Bootstrap::$main->user['id'] && !$event->img) { $event->img = $model->id; $event->save(); } $model->title = $event->name; $model->save(); } } } } return $this->status($ret); }