Beispiel #1
0
/**
 * Gets the jpeg contents of the resized version of an already uploaded image
 * (Returns false if the file was not an image)
 *
 * @param string $input_name The name of the file on the disk
 * @param int    $maxwidth   The desired width of the resized image
 * @param int    $maxheight  The desired height of the resized image
 * @param bool   $square     If set to true, takes the smallest of maxwidth and
 * 			                 maxheight and use it to set the dimensions on the new image.
 *                           If no crop parameters are set, the largest square that fits
 *                           in the image centered will be used for the resize. If square,
 *                           the crop must be a square region.
 * @param int    $x1         x coordinate for top, left corner
 * @param int    $y1         y coordinate for top, left corner
 * @param int    $x2         x coordinate for bottom, right corner
 * @param int    $y2         y coordinate for bottom, right corner
 * @param bool   $upscale    Resize images smaller than $maxwidth x $maxheight?
 *
 * @return false|mixed The contents of the resized image, or false on failure
 * @deprecated 2.3
 */
function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0, $upscale = false)
{
    elgg_deprecated_notice(__FUNCTION__ . ' has been deprecated. Use elgg_save_resized_image()', '2.3');
    if (!is_readable($input_name)) {
        return false;
    }
    // we will write resized image to a temporary file and then delete it
    $tmp_filename = time() . pathinfo($input_name, PATHINFO_BASENAME);
    $tmp = new ElggFile();
    $tmp->setFilename("tmp/{$tmp_filename}");
    $tmp->open('write');
    $tmp->close();
    $params = ['w' => $maxwidth, 'h' => $maxheight, 'x1' => $x1, 'y1' => $y1, 'x2' => $x2, 'y2' => $y2, 'square' => $square, 'upscale' => $upscale];
    $destination = $tmp->getFilenameOnFilestore();
    $image_bytes = false;
    if (elgg_save_resized_image($input_name, $destination, $params)) {
        $tmp->open('read');
        $image_bytes = $tmp->grabFile();
        $tmp->close();
    }
    $tmp->delete();
    return $image_bytes;
}
 /**
  * Saves an image on Elgg's filestore
  *
  * @param string $url URL of the image
  * @return \ElggFile|false
  */
 public function saveImageFromUrl($url)
 {
     $mime = $this->parser->getContentType($url);
     switch ($mime) {
         case 'image/jpeg':
         case 'image/jpg':
             $ext = 'jpg';
             break;
         case 'image/gif':
             $ext = 'gif';
             break;
         case 'image/png':
             $ext = 'png';
             break;
         default:
             return false;
     }
     $basename = sha1($url);
     $this->parser;
     $raw_bytes = $this->parser->read($url);
     if (empty($raw_bytes)) {
         return;
     }
     $site = elgg_get_site_entity();
     $tmp = new \ElggFile();
     $tmp->owner_guid = $site->guid;
     $tmp->setFilename("scraper_cache/tmp/{$basename}.{$ext}");
     $tmp->open('write');
     $tmp->write($raw_bytes);
     $tmp->close();
     unset($raw_bytes);
     $threshold = elgg_get_plugin_setting('cache_thumb_size_lower_threshold', 'hypeScraper', 100);
     $imagesize = getimagesize($tmp->getFilenameOnFilestore());
     if (!$imagesize || $imagesize[0] < $threshold) {
         $tmp->delete();
         return false;
     }
     $image = new \ElggFile();
     $image->owner_guid = $site->guid;
     $image->setFilename("scraper_cache/thumbs/{$basename}.jpg");
     $image->natural_width = $imagesize[0];
     $image->natural_height = $imagesize[1];
     $image->open('write');
     $image->close();
     $size = elgg_get_plugin_setting('cache_thumb_size', 'hypeScraper', 500);
     $thumb = elgg_save_resized_image($tmp->getFilenameOnFilestore(), $image->getFilenameOnFilestore(), ['w' => $size, 'h' => $size, 'upscale' => false, 'square' => false]);
     unset($thumb);
     $tmp->delete();
     return $image;
 }