GithubHelp home page GithubHelp logo

yiisoft / yii2-imagine Goto Github PK

View Code? Open in Web Editor NEW
289.0 43.0 95.0 235 KB

Yii 2 imagine extension

Home Page: http://www.yiiframework.com

License: BSD 3-Clause "New" or "Revised" License

PHP 97.40% Makefile 2.60%
yii yii2 images image imagine thumbnails hacktoberfest

yii2-imagine's Introduction

Imagine Extension for Yii 2


This extension adds most common image functions and also acts as a wrapper to Imagine image manipulation library for Yii framework 2.0.

For license information check the LICENSE-file.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

composer require --prefer-dist yiisoft/yii2-imagine

or add

"yiisoft/yii2-imagine": "~2.2.0"

to the require section of your composer.json.

Basic Usage

This extension is a wrapper to the Imagine and also adds the most commonly used image manipulation methods.

The following example shows how to use this extension:

use yii\imagine\Image;

// frame, rotate and save an image
Image::frame('path/to/image.jpg', 5, '666', 0)
    ->rotate(-8)
    ->save('path/to/destination/image.jpg', ['jpeg_quality' => 50]);

yii2-imagine's People

Contributors

andku83 avatar arhell avatar bizley avatar cebe avatar creocoder avatar gugoan avatar haruatari avatar klevron avatar klimov-paul avatar lesha701 avatar machour avatar mtangoo avatar nadar avatar pana1990 avatar qiangxue avatar renkas avatar samdark avatar schmunk42 avatar sdkiller avatar softark avatar sonicgd avatar terabytesoftw avatar yuniorsk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yii2-imagine's Issues

add feature to crop image durring resizing to avoid visual deffects

What steps will reproduce the problem?

I want to resize image with 2000x1100 dimensions to 1350x1290 without visual deffects. So, i write this code:

$resizedImageObject = Image::resize($originalImageSrc, $newWidth, $newHeight, false, true);

Original image:
db-website-avatars-02d

What's expected?

My image will resize to 2346x1290 and than crop left and right side, so i expected to get:
db-website-avatars-02d-1350x1290

What do you get instead?

My image was compressed in width and sctretched in height. So, I get:
resized-db-website-avatars-02d-1350x1290

Additional info

I solved this with this code:

$originalImageObject = Image::getImagine()->open($originalImageSrc);

$originalImageSizeObject = $originalImageObject->getSize();
$originalImageWidth = $originalImageSizeObject->getWidth();
$originalImageHeight = $originalImageSizeObject->getHeight();

$resizedImageWidth = ($newWidth / $newHeight < $originalImageWidth / $originalImageHeight) ? null : $newWidth;
$resizedImageHeight = ($newWidth / $newHeight > $originalImageWidth / $originalImageHeight) ? null : $newHeight;

$resizedImageObject = Image::resize($originalImageSrc, $resizedImageWidth, $resizedImageHeight, false, true);

$resizedImageSizeObject = $resizedImageObject->getSize();
$resizedImageWidth = $resizedImageSizeObject->getWidth();
$resizedImageHeight = $resizedImageSizeObject->getHeight();

$cropImagePointX = ($resizedImageWidth - $newWidth) / 2;
$cropImagePointY = ($resizedImageHeight - $newHeight) / 2;

$pointForCenterCropImage = new Point($cropImagePointX, $cropImagePointY);
$boxForCenterCropImage = new Box($newWidth, $newHeight);

$resizedImageObject->crop($pointForCenterCropImage, $boxForCenterCropImage);

This feature helps to resize image without visual deffects, to my mind it would be great to implement it in future updates, what do think about it?

Q A
Yii vesion 2.0.13
PHP version 7.1.9
Operating system Windows 10

Update or copy image and preserve exif meta data

How does the Yii2 Imagine handle exif data in an updated or new image?

Image::getImagine()->open($image)->save($output, ['quality' => 50]);

exif meta data will disappear, does not save the exif meta data of the $image

$exif = exif_read_data($image, 0, true);
If you can't update image, how can I add it $exif?

a new setter would solve the problem

Image::getImagine()->open($image)->setExif($exif)->save($output, ['quality' => 50]);

Q A
Yii version Yii2
Imagine version newest

thumbnail mode THUMBNAIL_INSET not centered

I'am trying to create thumbnails, and I need 'inset' mode, images are always at top left corner of thumbnails, and there is no way to make them centered. In 'outbound' mode images are centered.

Thumbnail method creates rectangle

Image::thumbnail($path, 100, 100)->save($thumbnail, ['quality' => 50]);

should create an image maintaining aspect ratio, but it does not.
It creates a rectangular - 100x100 px - image.

Class 'Imagine\\Image\\ManipulatorInterface' not found

I use composer to install the yii2-imagine, class dependent imagine/imagine is also installed, how is the not found exception

{"name":"PHP Fatal Error","message":"Class 'Imagine\Image\ManipulatorInterface' not found","code":1,"type":"yii\base\ErrorException","file":"/home/denghp/workspace/yii2-demo/vendor/yiisoft/yii2-imagine/BaseImage.php","line":149,"stack-trace":["#0 [internal function]: yii\base\ErrorHandler->handleFatalError()","#1 {main}"]}

update src folder link

What steps will reproduce the problem?

http=>https

What's expected?

What do you get instead?

Additional info

Q A
Yii version
Imagine version
PHP version
Operating system

Are there any plans to update the dependencies?

I'm working on website optimization for Google Page Speed, in particular on optimizing images.

For example, this works for jpg:

Image::getImagine()->open($filePath)->strip()->save($filePath, ['quality' => 85]);

but png is almost not compressed.

In Imagine version 0.7 has options png_compression_level and png_compression_filter that should help.

Change color depth in png images after saving

When I upload a png picture, it has a color depth of 8 bits and a size of 0.3 mb. After saving, the color depth becomes 32 bits, and the size with a maximum compression of 0.7 mb.

What steps will reproduce the problem?

  1. Create upload image form
  2. Add image saving code in the controller action
$file = UploadedFile::getInstanceByName('file');
Image::getImagine()->open($file->tempName)
    ->save('path/to/destination/image.png', ['png_compression_level' => 9]);
  1. Upload any phg image with 8 bit color depth and save it.

What's expected?

Expected png image with 8 bit color depth and it will be compressing

What do you get instead?

Get png image with 32 bit color depth and it has more size then first image.

Additional info

Q A
Yii version 2.0.15.1
Imagine version 2.2.0
PHP version 7.2
Operating system ubuntu 18.04

BaseImage::frame() possible bug

This issue has originally been reported by @rumatakira at yiisoft/yii2#10661.
Moved here by @samdark.


<?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            [
                'attribute' => 'photo',
                'value' => BaseImage::frame('/home/kira/lampstack-7.0.2-0/apache2/htdocs/mafia-yii2/frontend/tmp/1.jpg', $margin = 20, $color = $GLOBALS['statusColor']),
                'format' => ['image', ['width'=>'300']],
            ],
            ...

returns:

Not Found

The requested URL /mafia-yii2/frontend/web/�PNG was not found on this server.

Benefits of using this extension

Should the possible benefits of using this library documented somewhere - f.e the README? Imo each user should be able to see them right away.

Error while blur an image.

Try to use blur effect.

use yii\imagine\Image;

...
Image::getImagine()
                        ->open($original_image)
                        ->effects()->blur(3)
                        ->save($this->_uploads_directory . '/'. $altPath);
...

But receive next error:
imagefilter(): supplied resource is not a valid Image resource

if (false === imagefilter($this->resource, IMG_FILTER_GAUSSIAN_BLUR))

Is it my usage fail or module issue?

update docs folder link

What steps will reproduce the problem?

https=>https

What's expected?

What do you get instead?

Additional info

Q A
Yii version
Imagine version
PHP version
Operating system

How can I set and use just of `Gd2` library?

In default, use of first supported item of $driver array:
public static $driver = [self::DRIVER_GMAGICK, self::DRIVER_IMAGICK, self::DRIVER_GD2];
How can I set and use just of Gd2 library?

imagick installation

I installed imagine through composer, it throws imagick not installed. How to install it.

BaseImage misprints

Hello! I've found two misprints.

BaseImage.php lines 251 and 254 (function thumbnail)

$startX = ceil($width - $size->getWidth()) / 2;
...
$startY = ceil($height - $size->getHeight()) / 2;

I think, you need move closing brackets to end of expressions.

$startX = ceil(($width - $size->getWidth()) / 2);
...
$startY = ceil(($height - $size->getHeight()) / 2);

method resize with proportions

What steps will reproduce the problem?

http://stuff.cebe.cc/yii2docs-ru/yii-imagine-baseimage.html#resize%28%29-detail

Image::resize(Yii::getAlias('@webroot'.'/img/gallery/original/' . $file), 300, 
300, true, true)
->save(getAlias('@webroot'.'/img/gallery/300x300/' . $file), ['quality' => 80])

how do I properly use this method?

I need to resize the image and save the result.
If the image is larger - it needs to be reduced according to its proportions.
If the image is smaller - it needs to be increased according to the proportions

What's expected?

proportionally reduced or enlarged image

What do you get instead?

Error
Call to undefined method yii\imagine\Image::resize()

Additional info

Q A
Yii vesion 2.0.13-dev
PHP version 7.1.0
Operating system windows 10

problem with persian characters

i have a project with yii2
and i want to write customer name on a membership_cart that saved in image formt.
the characters saved successfully but it reversed. for example instead of "عباس" it save "س اب ع"

Using S3 Containers

I have a while working with this extension and never had the need of uploading the image directly to an S3 container.

I’m wondering if there’s a way of modifying the save() method to upload the file directly to S3 instead of saving locally and then upload to S3.

Specifically im doing Imagine::thumbnail($image, $width, $height)->save()

any ideas?

The script that uses the "Imagine" dying without any error messages.

Simply create new Imagine object then load image from string:

$fileContents = file_get_contents('http://ed-elaboration.com/files/previews/120_main_seliger.jpg');
$image = yii\imagine\Image::getImagine()->load($fileContents);
echo ' >> Loaded';

And the script will shut down without throw Exception. Message 'Loaded' doesn't appear.

Some files can be loaded but give same behavior when i'm trying crop it:
yii\imagine\Image::thumbnail($image, ....

That happening with some files. A link to the file that caused this error you can see in code above.

If I directly using the "Imagick" or GD, all files are processed without any errors.

Additional info

Q A
Yii vesion 2.0.10
PHP version 5.6.26-1~dotdeb+7.1 (cli)
Operating system Debian GNU/Linux 7.11 (wheezy)

read png steam error

What steps will reproduce the problem?

when i upgraded imagine 0.5.0 to 0.6.3, read png steam error occured.

get png steam from db(model Allianceinfo):

public function getlogo($logoid){  
		$db = new Query;  
		$db->select('id , logo , md5sum ,imageinfo')   
				->from('alliance_logo')  
				->where(['id' => $logoid])  
			;  
		return $db->one();  
}

Controller:

public function actionShowlogo()	{  
		$alliance_logo = AllianceInfo::getLogo('1');  
//print_r($alliance_logo);die;  
		return $this->renderPartial('logo' , [  
			'alliance_logo' => $alliance_logo,  
			]);  
	}

view logo.php

$logo = new \Imagine\Gd\Imagine;
$logo->read($alliance_logo['logo']);

$logo->show('png'); 

I got a Undefined index: uri error.

error message was :

web/vendor/imagine/imagine/lib/Imagine/Image/Metadata/AbstractMetadataReader.php at line 67
58596061626364656667686970717273747576

     *
     * @return string|null The URI f ava
     */
    private function getStreamMetadata($resource)
    {
        $metadata = array();
 
        if (false !== $data = @stream_get_meta_data($resource)) {
//			var_dump($data);die;
            $metadata['uri'] = $data['uri'];
            if (stream_is_local($resource)) {
                $metadata['filepath'] = realpath($data['uri']);
            }
        }
 
        return $metadata;
    }
 
    /**

the return value of stream_get_meta_data doesnt have uri index.

What do you get instead?

version 0.5.0 was worked fine.

Additional info

Q A
Yii vesion 2.0.10
PHP version 5.5.14
Operating system OpenSUSE 42.1

Create new release in order to support provided functions in the BaseImage class

What steps will reproduce the problem?

  1. use composer to installe yii2-imagine "yiisoft/yii2-imagine": "~2.0.0"
  2. try to use aspect ratio by using null as param: Image::thumbnail('path/to/image.jpg', 120, null);

What's expected?

Assuming ability to calculate sizes as described in code, but not yet released

What do you get instead?

an exception, as of course, null is not a valid value:

add image exception: Length of either side cannot be 0 or negative, current size is 120x

Additional info

Maybe you should make a new relase in order to support the described abilities in the code, otherwhise we have to integrate ^2.0@dev version of this very useful additional image class.

Q A
Yii vesion 2.0.8
PHP version PHP 7.0.4-7ubuntu2.1
Operating system Ubuntu

README needs update?

What steps will reproduce the problem?

Fallow the README.md and add "yiisoft/yii2-imagine": "~2.0.0"

What's expected?

Latest version of extension

What do you get instead?

Old 2.0.* version

so, README.md should says: "yiisoft/yii2-imagine": "^2.0.0", or sth?

"imagine/imagine": "0.5.*" is deprecated for imagic

PHP Fatal error: Undefined class constant 'COLOR_OPACITY' in /vendor/imagine/imagine/lib/Imagine/Imagick/Image.php on line 677

ImageMagick have updated 3 days ago
yii2-image using Imagine 0.5.0 (2013), most recent 0.6.3 (2015-09-19) and it dosen't use COLOR_OPACITY const.

when I try to update imagine/imagine to ^0.6.3 by composer it says: Problem 1
- yiisoft/yii2-imagine 2.0.3 requires imagine/imagine 0.5.* -> satisfiable by imagine/imagine[0.5.x-dev, v0.5.0] but these conflict with your requirements or minimum-stability.

Q A
Yii vesion 2.0.8
PHP version 5.5.17
Operating system Linux
imagick module version 3.4.3RC1
Imagick using ImageMagick library version ImageMagick 7.0.2-1 Q16 x86_64 2016-06-23

ExifMetadataReader::extractFromFile() does not return EXIF data

What steps will reproduce the problem?

(new ExifMetadataReader())->readFile($path)

What's expected?

Array populated with EXIF data. exif_read_data($path, null, true, false) works fine.

What do you get instead?

Array with only 'uri' and 'filepath'.

Additional info

Q A
Yii version 2.0.30
Imagine version 2.2.0
PHP version 7.3.11
Operating system Linux

Image::thumbnail with Upscaling?

What steps will reproduce the problem?

Use a Image smaller than 120x120 and create a Thumbnail with Box Option

Image::thumbnail('@webroot/img/test-image.jpg', 120, 120, ImageInterface::THUMBNAIL_INSET)
->save(Yii::getAlias('@runtime/thumb-test-image.jpg'), ['quality' => 50]);

What's expected?

The smaller Image should be placed inside the Box 120x120

What do you get instead?

A Thumbnail with the original size. Maybe this is intened, but if you specify the size, you expect the Image to be upscaled

Additional info

Q A
Yii vesion 2.0.10
Image Version 2.1.0
PHP version 5.6.27
Operating system Mac/Linux

Call to undefined method Imagick::setImageOpacity()

When i do:

Image::thumbnail( $newFile,
			self::BANNER_IMG_SIZE_WIDTH ,
			self::BANNER_IMG_SIZE_HEIGHT,
    		ManipulatorInterface::THUMBNAIL_INSET)
    		->save( $newFile,['quality' => 80]);

I got error:
``php
Error

Call to undefined method Imagick::setImageOpacity()

phpinfo():

php version : 7.0
imagick version : 3.4.3

yii2queue run job ,yii2-imagine not work;

What steps will reproduce the problem?

yii2queue extension run job
run this code ,it's not work and yii2queue still jobs down, +1,i can't debug
\yii\imagine\Image::watermark(Yii::getAlias('@webroot').'/thumb-watermark.jpg', '@webroot/slr-icon.png', [0,0])->save(Yii::getAlias('@webroot').'/uploads/watermark.jpg', ['quality' => 100]);

What's expected?

yii2queue create jobs can run that code
in controller that code is work ,but run yii2queue no work

What do you get instead?

Additional info

Q A
Yii vesion
yii2
PHP version
php5.6
Operating system
windows10
yii2-imagine ^2.1

Save operation failed Caused by: ImagickException

Imagine\Exception\RuntimeException

Save operation failed

Caused by: ImagickException

UnableToOpenBlob `uploads/products/xxxxx/thumbnail/65x65/6.jpg': No such file or directory @ error/blob.c/OpenBlob/2702

supplied resource is not a valid stream resource

$new_image does not exist.
Because it is created from the old file. (image)

Image::frame($image, 5, '666', 0)->rotate(-8)->save($new_image, ['jpeg_quality' => 50]);

Error:
PHP Warning 'yii\base\ErrorException' with message 'stream_get_contents(): supplied resource is not a valid stream resource'

in /*************/vendor/imagine/imagine/lib/Imagine/Gd/Imagine.php:119

Ignore `OutOfBoundsException` error when watermark grower of source image

This issue has originally been reported by @NabiKAZ at yiisoft/yii2#16612.
Moved here by @samdark.


When use this command:

Image::watermark('image.jpg', 'watermark.jpg')->save('image.jpg');

If dimension of watermark, grower of source image, return this error:

Exception 'Imagine\Exception\OutOfBoundsException' with message 'Cannot paste image of the given size at the specified position, as it moves outside of the current image's box' 

It's fine. But how can ignore this error, so new image will be create but the part of watermark that outside of the image box is hidden and removed.

php 7.3.6 segmentation fault

Image::thumbnail($url, 250 , 352);

    "yiisoft/yii2": "~2.0.6",
    "yiisoft/yii2-bootstrap": "~2.0.0",
    "yiisoft/yii2-swiftmailer": "~2.0.0 || ~2.1.0",
    "yiisoft/yii2-imagine": "~2.2.0",

Older versions of PHP won't have this problem

update github folder link

What steps will reproduce the problem?

http=>https

What's expected?

What do you get instead?

Additional info

Q A
Yii version
Imagine version
PHP version
Operating system

UnexpectedValueException when installing package

What steps will reproduce the problem?

composer require --prefer-dist yiisoft/yii2-imagine

What's expected?

package installed

What do you get instead?

UnexpectedValueException
Could not parse version constraint >=~2: Invalid version string "~2"

Additional info

Q A
Yii version 2.0.11
PHP version 7.1.0
Operating system Ubuntu 14.04.5 LTS 64-bit

After save image vertical photo rotated by horizontal

Hi
i upload file with UploadedFile and when save without this lib image have vertical orientation, but when i used jpeg_quality image rotated horizontal. Using GD library

$uploadFile = UploadedFile::getInstanceByName('file'.yii::$app->request->post('name'));
$uniq = uniqid().'.'.str_replace('image/','', $uploadFile->type);
$filePut = Helper::filePut( $uniq, file_get_contents( $uploadFile->tempName ));

Image::frame($filePut['path'])->save($filePut['path'], ['jpeg_quality' => 60]);

$file->content = file_get_contents($filePut['path']);

Can't create thumbnail from binary string

What steps will reproduce the problem?

$data = base64_decode($base64data);
yii\imagine\Image::Thumbnail((yii\imagine\Image::getImagine())->load($data), $somewidth, $someheight);

What's expected?

Expecting to create thumbnail from base64 decoded string.

What do you get instead?

InvalidParamException('File should be either ImageInterface, resource or a string containing file path.');

in file /vendor/yiisoft/yii2-imagine/BaseImage.php line 147

Additional info

Another problem I see in that same function, you check whether the image is instance of ImageInterface, but I think you must check for ImagineInterface instead, though this doesn't help with my case.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.