Exemplo n.º 1
1
 /**
  * Upload an image.
  *
  * @param $file
  * @return array
  */
 public function upload(array $file)
 {
     if (!$file) {
         $status = ['status' => 'error', 'message' => GENERIC_UPLOAD_ERROR_MESSAGE];
         return $status;
     }
     $tempName = $file['tmp_name'];
     if (is_null($tempName)) {
         $status = ['status' => 'error', 'message' => GENERIC_UPLOAD_ERROR_MESSAGE];
         return $status;
     }
     $imageInfo = getimagesize($tempName);
     if (!$imageInfo) {
         $status = ['status' => 'error', 'message' => 'Only images are allowed'];
         return $status;
     }
     $fileType = image_type_to_mime_type(exif_imagetype($tempName));
     if (!in_array($fileType, $this->_allowedTypes)) {
         $status = ['status' => 'error', 'message' => 'File type not allowed'];
         return $status;
     }
     $fileName = htmlentities($file['name']);
     $height = $this->_imagick->getImageHeight();
     $width = $this->_imagick->getImageWidth();
     $uploadPath = $_SERVER['DOCUMENT_ROOT'] . PROPERTY_IMG_TMP_DIR;
     if (!move_uploaded_file($tempName, $uploadPath . "/{$fileName}")) {
         $status = ['status' => 'error', 'message' => 'Can\'t move file'];
         return $status;
     }
     $status = ['status' => 'success', 'url' => PROPERTY_IMG_TMP_DIR . '/' . $fileName, 'width' => $width, 'height' => $height, 'token' => $_SESSION['csrf_token']];
     return $status;
 }
Exemplo n.º 2
0
 /**
  * The hard stuff. Resizing an image while applying an appropriate background
  * @param $sourceImage
  * @param $targetImage
  * @param $targetWidth
  * @param $targetHeight
  * @param int $quality
  * @throws Exception
  */
 public static function generate($sourceImage, $targetImage, $targetWidth, $targetHeight, $quality = 100)
 {
     // get source dimensions
     list($sourceWidth, $sourceHeight, $sourceMimeType) = getimagesize($sourceImage);
     // resolving mime type e.g. image/*
     $imageType = image_type_to_mime_type($sourceMimeType);
     // resolve Image Resource handler against the said image type and the source image path
     $sourceImageResource = static::getImageResourceFromImageTypeAndFile($imageType, $sourceImage);
     // resolve aspect-ratio maintained height and width for the thumbnail against source's dimensions and expected
     // dimensions
     list($calculatedTargetWidth, $calculatedTargetHeight) = static::resolveNewWidthAndHeight($sourceWidth, $sourceHeight, $targetWidth, $targetHeight);
     // create an image with the aspect-ration maintained height and width, this will be used to resample the source
     // image
     $resampledImage = imagecreatetruecolor(round($calculatedTargetWidth), round($calculatedTargetHeight));
     imagecopyresampled($resampledImage, $sourceImageResource, 0, 0, 0, 0, $calculatedTargetWidth, $calculatedTargetHeight, $sourceWidth, $sourceHeight);
     // create an image of the thumbnail size we desire (this may be less than the aspect-ration maintained height
     // and width
     $targetImageResource = imagecreatetruecolor($targetWidth, $targetHeight);
     // setup a padding color, in our case we use white.
     $paddingColor = imagecolorallocate($targetImageResource, 255, 255, 255);
     // paint the target image all in the padding color so the canvas is completely white.
     imagefill($targetImageResource, 0, 0, $paddingColor);
     // now copy the resampled aspect-ratio maintained resized thumbnail onto the white canvas
     imagecopy($targetImageResource, $resampledImage, ($targetWidth - $calculatedTargetWidth) / 2, ($targetHeight - $calculatedTargetHeight) / 2, 0, 0, $calculatedTargetWidth, $calculatedTargetHeight);
     // save the resized thumbnail.
     if (!imagejpeg($targetImageResource, $targetImage, $quality)) {
         throw new Exception("Unable to save new image");
     }
 }
 /**
  * Output the image to a browser
  */
 function output()
 {
     self::processImage();
     header('Content-Type: ' . image_type_to_mime_type($this->type));
     flush();
     imagejpeg($this->image, NULL, $this->quality);
 }
Exemplo n.º 4
0
 public function image_type_to_extension($imagetype)
 {
     if (empty($imagetype)) {
         return false;
     }
     switch ($imagetype) {
         case image_type_to_mime_type(IMAGETYPE_GIF):
             return 'gif';
         case image_type_to_mime_type(IMAGETYPE_JPEG):
             return 'jpg';
         case image_type_to_mime_type(IMAGETYPE_PNG):
             return 'png';
         case image_type_to_mime_type(IMAGETYPE_SWF):
             return 'swf';
         case image_type_to_mime_type(IMAGETYPE_PSD):
             return 'psd';
         case image_type_to_mime_type(IMAGETYPE_BMP):
             return 'bmp';
         case image_type_to_mime_type(IMAGETYPE_TIFF_II):
             return 'tiff';
         case image_type_to_mime_type(IMAGETYPE_TIFF_MM):
             return 'tiff';
         case 'image/pjpeg':
             return 'jpg';
         default:
             return false;
     }
 }
Exemplo n.º 5
0
/**
* 生成缩略图函数
* @param $filename string 源文件
* @param $dst_w int 目标的宽,px
* @param $dst_h int 目标的高,px    默认都为一半
* @param $savepath string 保存路径
* @param $ifdelsrc boolean 是否删除源文件
* @return 缩略图文件名
*/
function thumb($filename, $dst_w = null, $dst_h = null, $savepath = null, $ifdelsrc = false)
{
    list($src_w, $src_h, $imagetype) = getimagesize($filename);
    $scale = 0.5;
    if (is_null($dst_w) || is_null($dst_h)) {
        $dst_w = ceil($src_w * $scale);
        $dst_h = ceil($src_h * $scale);
    }
    $mime = image_type_to_mime_type($imagetype);
    $createfunc = str_replace('/', 'createfrom', $mime);
    $outfunc = str_replace('/', null, $mime);
    $src_image = $createfunc($filename);
    $dst_image = imagecreatetruecolor($dst_w, $dst_h);
    imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    if (is_null($savepath)) {
        $savepath = $_SERVER['DOCUMENT_ROOT'] . 'images/thumb';
    }
    if (!empty($savepath) && !is_dir($savepath)) {
        mkdir($savepath, 0777, true);
    }
    $fileinfo = pathinfo($filename);
    $savename = uniqid() . '.' . $fileinfo['extension'];
    if (!empty($savepath)) {
        $outfunc($dst_image, $savepath . '/' . $savename);
    } else {
        $outfunc($dst_image, $savename);
    }
    imagedestroy($src_image);
    imagedestroy($dst_image);
    if (!$ifdelsrc) {
        unlink($filename);
    }
    return $savename;
}
Exemplo n.º 6
0
/**
 * 生成缩略图
 * @param string $filename
 * @param string $destination
 * @param int $dst_w
 * @param int $dst_h
 * @param bool $isReservedSource
 * @param number $scale
 * @return string
 */
function thumb($filename, $destination = null, $dst_w, $dst_h = NULL, $isReservedSource = true)
{
    list($src_w, $src_h, $imagetype) = getimagesize($filename);
    if ($src_w <= $dst_w) {
        $dst_w = $src_w;
        $dst_h = $src_h;
    }
    if (is_null($dst_h)) {
        $dst_h = scaling($src_w, $src_h, $dst_w);
    }
    $mime = image_type_to_mime_type($imagetype);
    $createFun = str_replace("/", "createfrom", $mime);
    $outFun = str_replace("/", null, $mime);
    $src_image = $createFun($filename);
    $dst_image = imagecreatetruecolor($dst_w, $dst_h);
    imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    if ($destination && !file_exists(dirname($destination))) {
        mkdir(dirname($destination), 0777, true);
    }
    $dstFilename = $destination == null ? getUniName() . "." . getExt($filename) : $destination;
    $outFun($dst_image, $dstFilename);
    imagedestroy($src_image);
    imagedestroy($dst_image);
    if (!$isReservedSource) {
        unlink($filename);
    }
    return $dstFilename;
}
Exemplo n.º 7
0
 public static function getImageFromUrl($image_url)
 {
     try {
         $mime = image_type_to_mime_type(exif_imagetype($image_url));
     } catch (Exception $e) {
         throw new MimeTypeException($e->getMessage());
     }
     //Get image based on mime and set to $im
     switch ($mime) {
         case 'image/jpeg':
             $im = imagecreatefromjpeg($image_url);
             break;
         case 'image/gif':
             $im = imagecreatefromgif($image_url);
             break;
         case 'image/png':
             $im = imagecreatefrompng($image_url);
             break;
         case 'image/wbmp':
             $im = imagecreatefromwbmp($image_url);
             break;
         default:
             throw new MimeTypeException("An image of '{$mime}' mime type is not supported.");
             break;
     }
     return $im;
 }
 function fileupload_trigger_check()
 {
     if (intval(get_query_var('postform_fileupload')) == 1) {
         if (!(get_option('bbp_5o1_toolbar_allow_image_uploads') && (is_user_logged_in() || get_option('bbp_5o1_toolbar_allow_anonymous_image_uploads')))) {
             echo htmlspecialchars(json_encode(array("error" => __("You are not permitted to upload images.", 'bbp_5o1_toolbar'))), ENT_NOQUOTES);
             exit;
         }
         require_once dirname(__FILE__) . '/includes/fileuploader.php';
         // list of valid extensions, ex. array("jpeg", "xml", "bmp")
         $allowedExtensions = array('jpg', 'jpeg', 'png', 'gif');
         // Because using Extensions only is very bad.
         $allowedMimes = array(IMAGETYPE_JPEG, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);
         // max file size in bytes
         $sizeLimit = bbp_5o1_images_panel::return_bytes(min(array(ini_get('post_max_size'), ini_get('upload_max_filesize'))));
         $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
         $directory = wp_upload_dir();
         $result = $uploader->handleUpload(trailingslashit($directory['path']));
         $mime = exif_imagetype($result['file']);
         if (!$mime || !in_array($mime, $allowedMimes)) {
             $deleted = unlink($result['file']);
             echo htmlspecialchars(json_encode(array("error" => __("Disallowed file type.", 'bbp_5o1_toolbar'))), ENT_NOQUOTES);
             exit;
         }
         // Construct the attachment array
         $attachment = array('post_mime_type' => $mime ? image_type_to_mime_type($mime) : '', 'guid' => trailingslashit($directory['url']) . $result['filename'], 'post_parent' => 0, 'post_title' => $result['name'], 'post_content' => 'Image uploaded for a forum topic or reply.');
         // Save the data
         $id = wp_insert_attachment($attachment, $result['file'], 0);
         $result['id'] = $id;
         $result['attachment'] = $attachment;
         $result = array("success" => true, "file" => $attachment['guid']);
         echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
         exit;
     }
 }
Exemplo n.º 9
0
 public function actionShow()
 {
     $nome_da_pasta = $_GET['path'];
     $arquivo = $_GET['file'];
     $largura = $_GET['width'];
     $path = Yii::app()->fileManager->findFile($arquivo, $nome_da_pasta, $largura);
     if (!is_null($path)) {
         $info = getimagesize($path);
         $mime = image_type_to_mime_type($info[2]);
         switch ($mime) {
             case "image/jpeg":
                 $image = imagecreatefromjpeg($path);
                 header("Content-Type: image/jpeg");
                 imagejpeg($image);
                 break;
             case "image/png":
                 $image = imagecreatefrompng($path);
                 header("Content-Type: image/png");
                 imagepng($image);
                 break;
             case "image/gif":
                 $image = imagecreatefromgif($path);
                 header("Content-Type: image/gif");
                 imagegif($image);
                 break;
         }
     }
 }
Exemplo n.º 10
0
function thumb($filename, $destination = null, $dst_w = null, $dst_h = null, $isReservedSource = false, $scale = 0.5)
{
    list($src_w, $src_h, $imagetype) = getimagesize($filename);
    if (is_null($dst_w) || is_null($dst_h)) {
        $dst_w = ceil($src_w * $scale);
        $dst_h = ceil($src_h * $scale);
    }
    $mime = image_type_to_mime_type($imagetype);
    $createFun = str_replace("/", "createfrom", $mime);
    $outFun = str_replace("/", null, $mime);
    $src_image = $createFun($filename);
    $dst_image = imagecreatetruecolor($dst_w, $dst_h);
    imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    //image_50/sdfsdkfjkelwkerjle.jpg
    if ($destination && !file_exists(dirname($destination))) {
        mkdir(dirname($destination), 0777, true);
    }
    $dstFilename = $destination == null ? getUniName() . "." . getExt($filename) : $destination;
    $outFun($dst_image, $dstFilename);
    imagedestroy($src_image);
    imagedestroy($dst_image);
    if (!$isReservedSource) {
        unlink($filename);
    }
    return $dstFilename;
}
Exemplo n.º 11
0
 /**
  *  Resize an image to the specified dimensions, placing the resulting
  *  image in the specified location.  At least one of $newWidth or
  *  $newHeight must be specified.
  *
  *  @param  string  $type       Either 'thumb' or 'disp'
  *  @param  integer $newWidth   New width, in pixels
  *  @param  integer $newHeight  New height, in pixels
  *  @return string  Blank if successful, error message otherwise.
  */
 public static function ReSize($src, $dst, $newWidth = 0, $newHeight = 0)
 {
     global $_LGLIB_CONF;
     // Calculate the new dimensions
     $A = self::reDim($src, $newWidth, $newHeight);
     if ($A === false) {
         COM_errorLog("Invalid image {$src}");
         return 'invalid image conversion';
     }
     list($sWidth, $sHeight, $dWidth, $dHeight) = $A;
     // Get the mime type for the glFusion resizing functions
     $mime_type = image_type_to_mime_type(exif_imagetype($src));
     // Returns an array, with [0] either true/false and [1]
     // containing a message.
     $result = array();
     if (function_exists(_img_resizeImage)) {
         $result = _img_resizeImage($src, $dst, $sHeight, $sWidth, $dHeight, $dWidth, $mime_type);
     } else {
         $result[0] = false;
     }
     if ($result[0] == true) {
         return '';
     } else {
         COM_errorLog("Failed to convert {$src} ({$sHeight} x {$sWidth}) to {$dst} ({$dHeight} x {$dWidth})");
         return 'invalid image conversion';
     }
 }
Exemplo n.º 12
0
 public function upload()
 {
     if ($this->entityName === null || $this->entityId === null) {
         throw new \Exception('property entityName and entityId must be set to other than null');
     }
     $dir = $this->dir();
     if (!file_exists($dir)) {
         return false;
     }
     $path = $this->path();
     $tmp = $this->uploadData['tmp_name'];
     $check = getimagesize($tmp);
     if ($check === false) {
         return false;
     }
     switch ($check['mime']) {
         case image_type_to_mime_type(IMAGETYPE_GIF):
         case image_type_to_mime_type(IMAGETYPE_PNG):
         case image_type_to_mime_type(IMAGETYPE_JPEG):
             break;
         default:
             return false;
     }
     if (move_uploaded_file($tmp, $path)) {
         $this->thumbnail();
         return true;
     }
     return false;
 }
Exemplo n.º 13
0
function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale)
{
    list($imagewidth, $imageheight, $imageType) = getimagesize($image);
    $imageType = image_type_to_mime_type($imageType);
    $newImageWidth = ceil($width * $scale);
    $newImageHeight = ceil($height * $scale);
    $newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
    switch ($imageType) {
        case "image/png":
        case "image/x-png":
            $source = imagecreatefrompng($image);
            break;
    }
    imagecopyresampled($newImage, $source, 0, 0, $start_width, $start_height, $newImageWidth, $newImageHeight, $width, $height);
    switch ($imageType) {
        case "image/png":
        case "image/x-png":
            $transcol = imagecolorallocatealpha($newImage, 255, 0, 255, 127);
            $trans = imagecolortransparent($newImage, $transcol);
            imagefill($newImage, 0, 0, $transcol);
            imagesavealpha($newImage, true);
            imagealphablending($newImage, true);
            imagepng($newImage, $thumb_image_name);
            break;
    }
    chmod($thumb_image_name, 0777);
}
Exemplo n.º 14
0
 protected function getImage($image_url)
 {
     $this->url = $image_url;
     $mime = image_type_to_mime_type(exif_imagetype($image_url));
     $im;
     //Get image based on mime and set to $im
     switch ($mime) {
         case 'image/jpeg':
             $im = imagecreatefromjpeg($image_url);
             break;
         case 'image/gif':
             $im = imagecreatefromgif($image_url);
             break;
         case 'image/png':
             $im = imagecreatefrompng($image_url);
             break;
         case 'image/wbmp':
             $im = imagecreatefromwbmp($image_url);
             break;
         default:
             return NULL;
             break;
     }
     $this->image = $im;
     return $this;
 }
Exemplo n.º 15
0
 public function action_preview2()
 {
     $dir = $this->uploads_dir();
     // build image file name
     $filename = $this->request->param('filename');
     // check if file exists
     if (!file_exists($filename) or !is_file($filename)) {
         throw new HTTP_Exception_404('Picture not found');
     }
     $cachenamearr = explode('/', $filename);
     $name = array_pop($cachenamearr);
     $cachename = 'a2' . $name;
     $cachename = str_replace($name, $cachename, $filename);
     /** @var Image $image **/
     // trying get picture preview from cache
     if (($image = Cache::instance()->get($cachename)) === NULL) {
         // create new picture preview
         $image = Image::factory($filename)->resize(null, 50)->crop(50, 50)->render(NULL, 90);
         // store picture in cache
         Cache::instance()->set($cachename, $image, Date::MONTH);
     }
     // gets image type
     $info = getimagesize($filename);
     $mime = image_type_to_mime_type($info[2]);
     // display image
     $this->response->headers('Content-type', $mime);
     $this->response->body($image);
 }
Exemplo n.º 16
0
/**
 * 生成缩略图
 * @param string $imageName
 * @param float $scale
 */
function createImage($imageName, $scale = 0.5)
{
    print_r(getimagesize($imageName));
    //获取原始图片的宽度、高度、类型(数字)
    list($src_w, $src_h, $src_type) = getimagesize($imageName);
    //获取mime
    $mime = image_type_to_mime_type($src_type);
    echo $mime;
    $createFunc = str_replace("/", "createfrom", $mime);
    //imagecreatefromjpeg函数
    $outFunc = str_replace("/", null, $mime);
    //imagejpeg函数
    //读取源图片(使用变量函数)
    //$src_img = $createFunc($imageName);
    //使用变量函数
    $src_img = call_user_func($createFunc, $imageName);
    $dst_w = $scale * $src_w;
    $dst_h = $scale * $src_h;
    //创建目标图片
    $dst_img = imagecreatetruecolor($dst_w, $dst_h);
    //复制图片
    imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    //输出图片
    //$outFunc($dst_img, "small/" . $imageName);
    call_user_func($outFunc, $dst_img, "small/" . $imageName);
    //释放资源
    imagedestroy($src_img);
    imagedestroy($dst_img);
}
Exemplo n.º 17
0
 /**
  * Constructor. Opens an image.
  * @param string $filename File path
  * @param boolean $throwErrors [optional] If true will be throws exceptions
  * @throws InvalidParamException
  */
 public function __construct($filename, $throwErrors = true)
 {
     $filename = Yii::getAlias($filename);
     if (!is_readable($filename)) {
         $this->error = sprintf('Enable to read file: "%s"', $filename);
         if ($throwErrors) {
             throw new InvalidParamException($this->error);
         }
         return;
     }
     try {
         $info = getimagesize($filename);
     } catch (\Exception $e) {
         // Ignore
     }
     if (empty($info) || empty($info[0]) || empty($info[1])) {
         $this->error = sprintf('Bad image format: "%s"', $filename);
         if ($throwErrors) {
             throw new InvalidParamException($this->error);
         }
         return;
     }
     $this->filename = $filename;
     $this->width = intval($info[0]);
     $this->height = intval($info[1]);
     $this->type = $info[2];
     $this->mime = image_type_to_mime_type($this->type);
 }
Exemplo n.º 18
0
/**
 * Get image meta description for image ( basedir , baseurl , imgpath , extension , height , width , destpath , imagetype)
 * 
 * @param string $source URL source of image
 * @return array of image meta description 
 */
function jeg_get_image_meta($source)
{
    // meta image container
    $meta = array();
    // define upload path & dir
    $upload_info = wp_upload_dir();
    $meta['basedir'] = $upload_info['basedir'];
    $meta['baseurl'] = $upload_info['baseurl'];
    // check if image is local
    if (strpos($source, $meta['baseurl']) === false) {
        return false;
    } else {
        $destpath = str_replace($meta['baseurl'], '', $source);
        $meta['imgpath'] = $meta['basedir'] . $destpath;
        // check if img path exists, and is an image indeed
        if (!file_exists($meta['imgpath']) or !getimagesize($meta['imgpath'])) {
            return false;
        } else {
            // get image info
            $info = pathinfo($meta['imgpath']);
            $meta['extension'] = $info['extension'];
            // get image size
            $imagesize = getimagesize($meta['imgpath']);
            $meta['width'] = $imagesize[0];
            $meta['height'] = $imagesize[1];
            $meta['imagetype'] = image_type_to_mime_type($imagesize[2]);
            // now define dest path
            $meta['destpath'] = str_replace('.' . $meta['extension'], '', $destpath);
            // return this meta
            return $meta;
        }
    }
}
Exemplo n.º 19
0
 /**
  * @uses FileExtension
  */
 public function __construct($file, $do_exit = true)
 {
     list($width, $height, $mime_type, $attr) = getimagesize($file);
     if (!is_string($mime_type)) {
         $extension = new FileExtension($file);
         switch ($extension->__toString()) {
             case 'jpg':
             case 'jpeg':
                 $image_type = \IMAGETYPE_JPEG;
                 break;
             case 'gif':
                 $image_type = \IMAGETYPE_GIF;
                 break;
             case 'png':
                 $image_type = \IMAGETYPE_PNG;
                 break;
             default:
                 throw new \Exception("Could not determine image type from filename");
                 break;
         }
         $mime_type = image_type_to_mime_type($image_type);
     }
     header("Content-type: " . $mime_type);
     readfile($file);
     if ($do_exit) {
         exit;
     }
 }
 /**
  * Get the mime type as a string by passing the Image type as corresponding to one of the static 
  * class variables of the Image class
  */
 function imageTypeToMimeType($type)
 {
     if (function_exists('image_type_to_mime_type')) {
         $ret = image_type_to_mime_type($type);
     }
     return $ret;
 }
function resizeImage($SrcImage, $DestImage, $MaxWidth, $MaxHeight, $Quality)
{
    list($iWidth, $iHeight, $type) = getimagesize($SrcImage);
    $ImageScale = min($MaxWidth / $iWidth, $MaxHeight / $iHeight);
    $NewWidth = ceil($ImageScale * $iWidth);
    $NewHeight = ceil($ImageScale * $iHeight);
    $NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
    switch (strtolower(image_type_to_mime_type($type))) {
        case 'image/jpeg':
        case 'image/png':
        case 'image/gif':
            $NewImage = imagecreatefromjpeg($SrcImage);
            break;
        default:
            return false;
    }
    // Resize Image
    if (imagecopyresampled($NewCanves, $NewImage, 0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight)) {
        // copy file
        if (imagejpeg($NewCanves, $DestImage, $Quality)) {
            imagedestroy($NewCanves);
            return true;
        }
    }
}
Exemplo n.º 22
0
 function _load_data()
 {
     if (!$this->loaded && $this->havegd) {
         if (!empty($this->filename) && is_file($this->filename)) {
             $this->format = strtolower(substr($this->filename, strrpos($this->filename, '.') + 1));
             list($this->width, $this->height, $type) = getimagesize($this->filename);
             if (function_exists("image_type_to_extension")) {
                 $this->format = image_type_to_extension($type, false);
             } else {
                 $tmp = image_type_to_mime_type($type);
                 $this->format = strtolower(substr($tmp, strrpos($tmp, "/") + 1));
             }
             if ($this->is_supported($this->format)) {
                 if ($this->format == 'jpg') {
                     $this->format = 'jpeg';
                 }
                 $this->data = call_user_func('imagecreatefrom' . $this->format, $this->filename);
                 $this->loaded = true;
             }
         } elseif (!empty($this->data)) {
             $this->data = imagecreatefromstring($this->data);
             $this->loaded = true;
         }
     }
 }
Exemplo n.º 23
0
function thumb($filename, $destination = null, $dst_w = null, $dst_h = null, $isReservedSource = true, $scale = 0.5)
{
    //getimagesize() —— 取得图片的长宽。
    list($src_w, $src_h, $imagetype) = getimagesize($filename);
    if (is_null($dst_w) || is_null($dst_h)) {
        $dst_w = ceil($src_w * $scale);
        $dst_h = ceil($src_h * $scale);
    }
    $mime = image_type_to_mime_type($imagetype);
    $createFun = str_replace("/", null, $mime);
    $outFun = str_replace("/", null, $mime);
    $src_image = $createFun($filename);
    $dst_image = imagecreatetruecolor($dst_w, $dst_h);
    //imagecopyresampled — 重采样拷贝部分图像并调整大小
    imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    if ($destination && !file_exists(dirname($destination))) {
        mkdir(dirname($destination), 0777, TRUE);
    }
    $dstFilename = $destination = null ? getUniName() . "." . getExt($filename) : $destination;
    $outFun($dst_image, $dstFilename);
    imagedestroy($src_image);
    imagedestroy($dst_image);
    if (!$isReservedSource) {
        unlink($filename);
    }
    return $dstFilename;
}
Exemplo n.º 24
0
 public function send($type = Image::JPEG, $quality = NULL)
 {
     if ($type !== Image::GIF && $type !== Image::PNG && $type !== Image::JPEG) {
         throw new InvalidArgumentException("Unsupported image type.");
     }
     header('Content-Type: ' . image_type_to_mime_type($type));
     return $this->save(NULL, $quality, $type);
 }
Exemplo n.º 25
0
 function mime()
 {
     if ($this->image_type) {
         return image_type_to_mime_type($this->image_type);
     } else {
         return 'image/';
     }
 }
Exemplo n.º 26
0
 public function actionView($url)
 {
     $full = "./images/uploads/" . str_replace("..", "", $url);
     header('Content-Type: ' . image_type_to_mime_type(exif_imagetype($full)));
     header('Content-Length: ' . filesize($full));
     echo file_get_contents($full);
     die;
 }
Exemplo n.º 27
0
 /**
  * @return string
  */
 public function getMimeType()
 {
     if (!file_exists($this->getPath())) {
         throw new \Exception('File ' . $this->getPath() . ' does not exist');
     }
     $image = getimagesize($this->getPath());
     return image_type_to_mime_type($image[2]);
 }
Exemplo n.º 28
0
 public function getMimeType()
 {
     $imagetype = exif_imagetype($this->outputFile);
     if ($imagetype) {
         return image_type_to_mime_type($imagetype);
     }
     return 'image/jpeg';
 }
Exemplo n.º 29
0
function common_slika()
{
    global $conf_files_path, $user_nastavnik, $user_studentska, $user_siteadmin, $userid;
    // Poslani parametar
    $osoba = intval($_REQUEST['osoba']);
    $promjena = intval($_REQUEST['promjena']);
    // Studenti mogu vidjeti samo svoju sliku
    if (!$user_nastavnik && !$user_studentska && !$user_siteadmin && $osoba != $userid) {
        niceerror("Možete vidjeti samo svoju sliku");
        zamgerlog("pristupa slici za osobu {$osoba} a student je", 3);
        zamgerlog2("pristupa tudjoj slici a student je", $osoba);
        return;
    }
    if ($promjena == 1) {
        $q = myquery("select slika from promjena_podataka where osoba={$osoba}");
    } else {
        $q = myquery("select slika from osoba where id={$osoba}");
    }
    if (mysql_num_rows($q) < 1) {
        // Ova poruka se neće vidjeti iz <img> taga, ali neko može otvoriti sliku u posebnom prozoru/tabu
        niceerror("Nepostojeća osoba {$osoba}");
        zamgerlog("slika: nepostojeca osoba {$osoba}", 3);
        zamgerlog2("nepostojeca osoba", $osoba);
        return;
    }
    $slika = mysql_result($q, 0, 0);
    if ($slika == "") {
        niceerror("Osoba {$osoba} nema sliku");
        zamgerlog("osoba u{$osoba} nema sliku", 3);
        zamgerlog2("osoba nema sliku", $osoba);
        return;
    }
    $lokacija_slike = "{$conf_files_path}/slike/{$slika}";
    if (!file_exists($lokacija_slike)) {
        niceerror("Slika za osobu {$osoba} je definisana, ali datoteka ne postoji");
        zamgerlog("nema datoteke za sliku osobe u{$osoba}", 3);
        zamgerlog2("nema datoteke za sliku", $osoba);
        return;
    }
    // Odredjujemo mimetype
    $podaci = getimagesize($lokacija_slike);
    $mimetype = image_type_to_mime_type($podaci[2]);
    if ($mimetype == "") {
        niceerror("Nepoznat tip slike za osobu {$osoba}");
        zamgerlog("nepoznat tip slike za osobu u{$osoba}", 3);
        zamgerlog2("nepoznat tip slike", $osoba);
        return;
    }
    header("Content-Type: {$mimetype}");
    $k = readfile($lokacija_slike, false);
    if ($k == false) {
        //print "Otvaranje slike nije uspjelo! Kontaktirajte administratora";
        // Pošto je header već poslan, nema smisla ispisivati grešku
        zamgerlog("citanje fajla za sliku nije uspjelo u{$osoba}", 3);
        zamgerlog2("citanje fajla za sliku nije uspjelo", $osoba);
    }
    exit;
}
Exemplo n.º 30
0
function redimensionner_image($image_source, $largeur_max, $hauteur_max, $sauvegarder = false, $image_destination = '', $fix = false, $fix_couleur = 'f00')
{
    list($largeur_source, $hauteur_source, $type_source, ) = getimagesize($image_source);
    $mime_source = image_type_to_mime_type($type_source);
    $largeur_final = $largeur_max;
    $hauteur_final = $hauteur_max;
    /* Origine de l'image : Par défaut à zéro */
    $origine_x = 0;
    $origine_y = 0;
    if ($largeur_source < $largeur_final) {
        $largeur_final = $largeur_source;
    }
    // Teste les dimensions tenant dans la zone
    $test_h = round($largeur_final / $largeur_source * $hauteur_source);
    $test_w = round($hauteur_final / $hauteur_source * $largeur_source);
    if (!$hauteur_final) {
        // Si Height final non précisé (0)
        $hauteur_final = $test_h;
    } elseif (!$largeur_final) {
        // Sinon si Width final non précisé (0)
        $largeur_final = $test_w;
    } elseif ($test_h > $hauteur_final) {
        // Sinon test quel redimensionnement tient dans la zone
        $largeur_final = $test_w;
    } else {
        $hauteur_final = $test_h;
    }
    if ($mime_source == 'image/jpeg') {
        $img_in = imagecreatefromjpeg($image_source);
    } elseif ($mime_source == 'image/png') {
        $img_in = imagecreatefrompng($image_source);
    } elseif ($mime_source == 'image/gif') {
        $img_in = imagecreatefromgif($image_source);
    } else {
        return false;
    }
    if ($fix) {
        $img_out = imagecreatetruecolor($largeur_max, $hauteur_max);
        if ($largeur_final < $largeur_max) {
            $origine_x = round(($largeur_max - $largeur_final) / 2);
        }
        if ($hauteur_final < $hauteur_max) {
            $origine_y = round(($hauteur_max - $hauteur_final) / 2);
        }
        $couleurs = hex2rgb($fix_couleur);
        $fond = ImageColorAllocate($img_out, $couleurs[0], $couleurs[1], $couleurs[2]);
        imagefill($img_out, 0, 0, $fond);
    } else {
        $img_out = imagecreatetruecolor($largeur_final, $hauteur_final);
    }
    imagecopyresampled($img_out, $img_in, $origine_x, $origine_y, 0, 0, $largeur_final, $hauteur_final, imagesx($img_in), imagesy($img_in));
    if ($sauvegarder) {
        imagejpeg($img_out, $image_destination);
    } else {
        imagejpeg($img_out, null, 100);
    }
}