/** * Get image size * * @return array * @throws \Exception */ public function getSize() { $result = $this->analyzer->getSize(); if (!is_array($result)) { // die('<pre>'.print_r($this->analyzer, true).'</pre>'); throw new \Exception('Unable to get image size.'); } return $result; }
/** * Get dimensions of remote images * @param String $url The image URL * @return Array Image height and width * @link http://stackoverflow.com/a/4635991 */ function keel_petfinder_api_get_image_dimensions($url = null) { if (empty($url)) { return; } // Get image dimensions $image = new FastImage($url); list($width, $height) = $image->getSize(); return array('height' => $height, 'width' => $width); }
public static function size($path = false) { if (empty($path)) { return array(0, 0); } require_once ROOT_PATH . 'libs/Fastimage.php'; $image = new FastImage($path); return $image->getSize(); }
public static function extract_by_downloading_image($dimensions, $url) { if (is_array($dimensions)) { return $dimensions; } $url_hash = md5($url); $transient_name = sprintf('amp_img_%s', $url_hash); $transient_expiry = 30 * DAY_IN_SECONDS; $transient_fail = 'fail'; $dimensions = get_transient($transient_name); if (is_array($dimensions)) { return $dimensions; } elseif ($transient_fail === $dimensions) { return false; } // Very simple lock to prevent stampedes $transient_lock_name = sprintf('amp_lock_%s', $url_hash); if (false !== get_transient($transient_lock_name)) { return false; } set_transient($transient_lock_name, 1, MINUTE_IN_SECONDS); // Note to other developers: please don't use this class directly as it may not stick around forever... if (!class_exists('FastImage')) { require_once AMP__DIR__ . '/includes/lib/class-fastimage.php'; } // TODO: look into using curl+stream (https://github.com/willwashburn/FasterImage) $image = new FastImage($url); $dimensions = $image->getSize(); if (!is_array($dimensions)) { set_transient($transient_name, $transient_fail, $transient_expiry); delete_transient($transient_lock_name); return false; } set_transient($transient_name, $dimensions, $transient_expiry); delete_transient($transient_lock_name); return $dimensions; }
<?php require '../Fastimage.php'; $uri = 'http://pcdn.500px.net/8123858/7051e2440a869a3fec74406a3aa200618452c390/4.jpg'; echo "\n\n"; $time = microtime(true); $image = new FastImage($uri); list($width, $height) = $image->getSize(); echo "FastImage: \n"; echo "Width: " . $width . "px Height: " . $height . "px in " . (microtime(true) - $time) . " seconds \n"; $time = microtime(true); list($width, $height) = getimagesize($uri); echo "getimagesize: \n"; echo "Width: " . $width . "px Height: " . $height . "px in " . (microtime(true) - $time) . " seconds \n"; exit;
static function getImages($text, $url, $imageQuantity) { $content = array(); // get all images from img src if (preg_match_all(Regex::$imageRegex, $text, $matching)) { for ($i = 0; $i < count($matching[0]); $i++) { $src = ""; $pathCounter = substr_count($matching[0][$i], "../"); preg_match(Regex::$srcRegex, $matching[0][$i], $imgSrc); $imgSrc = Url::canonicalImgSrc($imgSrc[2]); if (!preg_match(Regex::$httpRegex, $imgSrc)) { $src = Url::getImageUrl($pathCounter, Url::canonicalLink($imgSrc, $url)); } if ($src . $imgSrc != $url) { if ($src == "") { array_push($content, $src . $imgSrc); } else { array_push($content, $src); } } } } // get all full image urls from anywhere on the page if (preg_match_all(Regex::$urlRegex, $text, $matching)) { for ($i = 0; $i < count($matching[0]); $i++) { if (self::isImage($matching[0][$i])) { $content[] = $matching[0][$i]; } } } $content = array_unique($content); $content = array_values($content); $maxImages = $imageQuantity != -1 && $imageQuantity < count($content) ? $imageQuantity : count($content); $images = array(); for ($i = 0; $i < count($content); $i++) { try { $image = new FastImage($content[$i]); list($width, $height) = $image->getSize(); if ($width > 120 && $height > 120) { // avoids getting very small images $images[] = $content[$i]; $maxImages--; if ($maxImages == 0) { break; } } } catch (\Exception $ex) { error_log("Skipping {$content[$i]}"); } // skip images that can't be fetched } return $images; }
<?php require '../Fastimage.php'; $uri = "https://dotjpg.co/JVX1.jpg"; $time = microtime(true); $stream = fopen($uri, 'r'); $image = FastImage::withStream($stream); list($width, $height) = $image->getSize(); echo "FastImage: <br>"; echo "Width: " . $width . "px<br>Height: " . $height . "px<br>in " . (microtime(true) - $time) . " seconds <br><br>"; $time = microtime(true); list($width, $height) = getimagesize($uri); echo "getimagesize: <br>"; echo "Width: " . $width . "px<br>Height: " . $height . "px<br>in " . (microtime(true) - $time) . " seconds <br>"; exit;
/** * Fetch images via FastImage library * * @param array $urls_to_fetch Image src urls to fetch. * @param array $images Array to populate with results of image/dimension inspection. */ private static function fetch_images_via_fast_image($urls_to_fetch, &$images) { require_once AMP__DIR__ . '/includes/lib/class-fastimage.php'; $image = new FastImage(); $urls = array(); // array_column doesn't exist in PHP 5.2. foreach ($urls_to_fetch as $key => $value) { $urls[] = $key; } foreach ($urls as $url) { $result = $image->load($url); if (false === $result) { $images[$url]['size'] = self::STATUS_IMAGE_EXTRACTION_FAILED; } else { $size = $image->getSize(); $images[$url]['size'] = $size; } } }
function wm_image_resize($width, $height, $img_url, $crop = '') { global $data; ////check portrait or landscape//// $path_arr = parse_url($img_url); $hostname = $path_arr["scheme"] . "://" . $path_arr["host"]; $img_url_replace = str_replace($hostname, "", $img_url); /* $size = getimagesize($img_url); $width_img = $size[0]; $height_img = $size[1]; */ //$_SERVER['DOCUMENT_ROOT'].$img_url_replace $width_img = 0; $height_img = 0; if (function_exists('mb_convert_encoding') && ini_get('allow_url_fopen')) { $image = new FastImage($img_url); list($width_img, $height_img) = $image->getSize(); } if ($width_img > 0 && $height_img > 0) { $aspect = $height_img / $width_img; if ($aspect >= 1) { $crop_mode = false; } else { $crop_mode = true; } //landscape } else { $crop_mode = true; } ////// End ///////// if ($data["wm_thumbnail_generator"] == "Enable Timthumb") { //TimThumb global $blog_id; $quality = 100; $url = get_template_directory_uri(); /////crop image location //// if ($crop != '') { $timthumb_crop_location = $crop; } else { $timthumb_crop_location = $data["wm_timthumb_crop_location"]; } $query_crop_location = ""; if ($timthumb_crop_location == 'Align top') { $query_crop_location = '&a=t'; } elseif ($timthumb_crop_location == 'Align top right') { $query_crop_location = '&a=tr'; } elseif ($timthumb_crop_location == 'Align top left') { $query_crop_location = '&a=tl'; } elseif ($timthumb_crop_location == 'Align bottom') { $query_crop_location = '&a=b'; } elseif ($timthumb_crop_location == 'Align bottom right') { $query_crop_location = '&a=br'; } elseif ($timthumb_crop_location == 'Align bottom left') { $query_crop_location = '&a=bl'; } elseif ($timthumb_crop_location == 'Align left') { $query_crop_location = '&a=l'; } elseif ($timthumb_crop_location == 'Align right') { $query_crop_location = '&a=r'; } //////////// end crop location //////// if ($r_resize == 'yes' || !isset($r_resize)) { //There's a thumbnail image set, so check if we're on WPMU or WP if (isset($blog_id) && $blog_id > 0) { //We're on WPMU $imageParts = explode('/files/', $img_url); if (isset($imageParts[1])) { $img_url = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1]; $img_url = $url . '/timthumb.php?src=' . $img_url . '&w=' . $width . '&h=' . $height . '&zc=1&q=' . $quality; } else { $img_url = $url . '/timthumb.php?src=' . $img_url . '&w=' . $width . '&h=' . $height . '&zc=1&q=' . $quality; } } else { $img_url = $url . '/timthumb.php?src=' . $img_url . '&w=' . $width . '&h=' . $height . '&zc=1&q=' . $quality; } } else { $img_url = $img_url; } $img_url = $img_url . $query_crop_location; } elseif ($data["wm_thumbnail_generator"] == "Enable WP Image Resize") { //WP Image Resize /*$image = vt_resize('', $img_url, $width, $height,true); $img_url = $image['url'];*/ $img_url = aq_resize($img_url, $width, $height, true, true, true); } else { //default resize $img_url = aq_resize($img_url, $width, $height, true, true, true); } return urldecode($img_url); }