Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with the tags versions #1

Closed
mapirelli opened this issue Feb 12, 2016 · 14 comments
Closed

Problem with the tags versions #1

mapirelli opened this issue Feb 12, 2016 · 14 comments

Comments

@mapirelli
Copy link

Hi,

this porting is very usefull, i love the new methods, but i found that the tag version 3.0.* released differs from the master and the documentation.
I downloaded the library with composer
Mariano

@SmetDenis
Copy link
Member

Hi Mariano,

Thanks a lot!

Yes stable tag is not released yet, because I'm develop it now.
But you can download dev version via composer.

composer require jbzoo/image:"3.x-dev"

Best regards, Denis.

@mapirelli
Copy link
Author

Hi Denis,
i understand, thanks.

i report you a little writing mistake on line 331 in the dev version:

$cleanFilename = FS::clean($filename);
        if (!file_exists($cleanFilename)) {
            throw new Exception('Image file not forund: ' . $filename);
        }

And then i want suggest you to develop a feature that can be usefull:
The possibility to load an image from string and not only from a file, this can be usefull when we want manipulate images loaded from database. Now i extended your class and i overload the constructor in order to load the image. I want share with you these few lines of code:

/**
 * Enter description here ...
 * @author Mariano.Pirelli
 *
 */
class ImageMemory extends Image {

    public function __construct($imageString) {

        // Require GD library
        if (!extension_loaded('gd')) {
            throw new \Exception('Required extension GD is not loaded.');
        }

        if (empty($imageString)) {
            throw new \Exception('Empty image string data');
        }

        if (function_exists('getimagesizefromstring')) {
            //gather meta data
            $info = getimagesizefromstring($imageString);
        } else {
            throw new \Exception('PHP 5.4 is required to use method getimagesizefromstring');
        }

        $this->cleanup();
        $this->_mime   = $info['mime'];
        $this->_width  = $info[0];
        $this->_height = $info[1];
        $this->_image = imagecreatefromstring($imageString);
        $this->_exif   = array();
        $this->_orient = $this->_getOrientation();

        imagesavealpha($this->_image, true);
        imagealphablending($this->_image, true);

        return $this;
    }
}

Thanks for the work.
Best regards
Mariano

@SmetDenis
Copy link
Member

Yea, thanks.
I think it can be usefull, I will add support to new release.

But I think that saving images to database is nt good idea.

@mapirelli
Copy link
Author

Oh yes i know...there are advantage and disadvantage..
I forgot to add the render method getImageData();

public function getImageData($format = 'png', $quality = null)
{
        if (!$this->_image) {
            throw new \Exception('Image resource not defined');
        }
        // Output the image
        ob_start();
        $mimetype  = $this->_renderImageByFormat($format, null, $quality);
        $imageData = ob_get_contents();
        ob_end_clean();

        return $imageData;
}

@SmetDenis
Copy link
Member

Great!

PS I changed your comments, fixed highlight of code.

@mapirelli
Copy link
Author

Thank you very much.

I want suggest you just another thing. i hope that this can help:

I found that PHP under windows use the extension .tmp on uploaded files, included images, while others system don't use the extension.

So if i use a code like this an exception will be thrown:

$image = new Image($_FILES['name']['tmp_name']); // the temp file
$image->bestFit(800, 600)->save(); //save the temp file
copy($_FILES['name']['tmp_name'], $newPath); //copy the file 

the exception is: Undefined format. because the format is tmp.

    protected function _renderImageByFormat($format, $filename, $quality)
    {
        if (Helper::isJpeg($format)) {
        } elseif (Helper::isPng($format)) {
        } elseif (Helper::isGif($format)) {
        } else {
            throw new Exception('Undefined format: ' . $format);
        }
        return $result;
    }

so i suggest this behavior:

when the method save() is called, could be better read the image format from the mime type instead from file extension, the filename is not changed so also the format, and the error above will be prevented.

while on savaAs(), read like now the format from the file name extension.

i hope that is comprensible what i wrote.

Mariano

@SmetDenis
Copy link
Member

Hi Mariano,

I jsut pushed new version to 3.x-dev. Please try it and tell me what do you think.

Thanks.

@SmetDenis
Copy link
Member

All these features available in the 3.x version. It is stable now.
Thanks.

@mapirelli
Copy link
Author

Hi Denis,
thanks for the new features, looks perfect.
I don't found the method to get the modified image in string format, must i use the base64 function and then convert the result ?

@SmetDenis
Copy link
Member

Hi,

You can use $string = $image->getBase64(<FORMAT>)
or $image->getImage() to convert string manually.

May be you can sugget me some another method to convert it to string? May be with pull request?

Best regards, Denis.

@SmetDenis SmetDenis reopened this Feb 19, 2016
@mapirelli
Copy link
Author

Hi,
like the base64 without convertion, the binary format is:

    /**
     * Outputs image as binary string 
     *
     * @param null|string $format  If omitted or null - format of original file will be used, may be gif|jpg|png
     * @param int|null    $quality Output image quality in percents 0-100
     * @return string
     *
     * @throws Exception
     */
    public function getImageData($format = 'gif', $quality = null) or
    public function getImageString($format = 'gif', $quality = null) or
    public function getImageBinary($format = 'gif', $quality = null)
    {
        if (!$this->_image) {
            throw new Exception('Image resource not defined');
        }
        // Output the image
        ob_start();
        $mimetype  = $this->_renderImageByFormat($format, null, $quality);
        $imageData = ob_get_contents();
        ob_end_clean();
        return $imageData;
    }

@SmetDenis
Copy link
Member

Hi mapirelli,

Try to use getBinary() from v3.0.2

@mapirelli
Copy link
Author

Hi Denis, thank you very much.

@SmetDenis
Copy link
Member

You are welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants