Beispiel #1
0
 /**
  * load image
  *
  * @return bool
  * @param string $url source url
  * @param string $extension file extension of output file
  * @param int $width
  * @param int $height
  */
 public function loadImage($url, $extension = 'png', $width = false, $height = false)
 {
     // load image
     try {
         $data = \helpers\WebClient::request($url);
     } catch (\exception $e) {
         \F3::get('logger')->log("failed to retrieve image {$url}," . $e->getMessage(), \ERROR);
         return false;
     }
     // get image type
     $tmp = \F3::get('cache') . '/' . md5($url);
     file_put_contents($tmp, $data);
     $imgInfo = @getimagesize($tmp);
     if (strtolower($imgInfo['mime']) == 'image/vnd.microsoft.icon') {
         $type = 'ico';
     } elseif (strtolower($imgInfo['mime']) == 'image/png') {
         $type = 'png';
     } elseif (strtolower($imgInfo['mime']) == 'image/jpeg') {
         $type = 'jpg';
     } elseif (strtolower($imgInfo['mime']) == 'image/gif') {
         $type = 'gif';
     } elseif (strtolower($imgInfo['mime']) == 'image/x-ms-bmp') {
         $type = 'bmp';
     } else {
         @unlink($tmp);
         return false;
     }
     // convert ico to png
     if ($type == 'ico') {
         $ico = new \floIcon();
         @$ico->readICO($tmp);
         if (count($ico->images) == 0) {
             @unlink($tmp);
             return false;
         }
         ob_start();
         @imagepng($ico->images[count($ico->images) - 1]->getImageResource());
         $data = ob_get_contents();
         ob_end_clean();
     }
     // parse image for saving it later
     @unlink($tmp);
     try {
         $wideImage = \WideImage::load($data);
     } catch (\Exception $e) {
         return false;
     }
     // resize
     if ($width !== false && $height !== false) {
         if ($height !== null && $wideImage->getHeight() > $height || $width !== null && $wideImage->getWidth() > $width) {
             $wideImage = $wideImage->resize($width, $height);
         }
     }
     // return image as jpg or png
     if ($extension == 'jpg') {
         $data = $wideImage->asString('jpg', 75);
     } else {
         $data = $wideImage->asString('png', 4, PNG_NO_FILTER);
     }
     return $data;
 }
Beispiel #2
0
 /**
  * get JSON object
  * @param string $url URL
  * @return object JSON object
  */
 public function getJsonContent($url)
 {
     $content = null;
     try {
         $content = \helpers\WebClient::request($url);
     } catch (\exception $e) {
         throw new \exception('github spout error ' . $e->getMessage());
     }
     $json = @json_decode($content);
     if (empty($json)) {
         throw new \exception('github spout error: empy json');
     }
     return $json;
 }