yurkinx / yii2-image

Yii2 Framework extension for image manipulating using Kohana Image Library
65 stars 35 forks source link

Black strips on resized image #13

Closed alexweb-zz closed 9 years ago

alexweb-zz commented 9 years ago

When I'm resizing image some strange black borders appear on thumbnail. My code is:

            /*
             * Save thumbnail image
             */
            $thumbPath=$dir . '/' . self::getThumbPrefix() . $image->name;
            $saved=Yii::$app->image->load($path)
                ->resize(self::THUMB_WIDTH, self::THUMB_HEIGHT, yii\image\drivers\Image::INVERSE)
                ->crop(self::THUMB_WIDTH, self::THUMB_HEIGHT)
                ->save($thumbPath, $quality = 100);
            if($saved==false)
                throw new HttpException(500, 'Unable to save image thumbnail.');

Original image:

reczycz_belapan150714-4r4ub

Final image:

224x224_1_1436904097

Please check top and bottom borders. Do you know why does it happen?

alexweb-zz commented 9 years ago

Ok, I found the reason. The problem was not in your library. Before saving image I fix image orientation using this method (it rotates image according to EXIF data):

    public static function fixImageOrientation(UploadedFile $file)
    {
        if(!empty($file->tempName)
            && exif_imagetype($file->tempName) == IMAGETYPE_JPEG){
            $image = imagecreatefromjpeg($file->tempName);
            $image = imagerotate($image, array_values([0, 0, 0, 180, 0, 0, -90, 0, 90])[@exif_read_data($file->tempName)['Orientation'] ?: 0], 0);
            imagejpeg($image, $file->tempName);
        }
    }

And that method adds these strips...