o2system /
image
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System\Image\Drivers; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Image\Abstracts\AbstractDriver; |
||||
| 19 | use O2System\Image\Abstracts\AbstractWatermark; |
||||
| 20 | use O2System\Image\Dimension; |
||||
| 21 | use O2System\Image\Watermark\Overlay; |
||||
| 22 | use O2System\Image\Watermark\Text; |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * Class GdDriver |
||||
| 26 | * |
||||
| 27 | * @package O2System\Image\Drivers |
||||
| 28 | */ |
||||
| 29 | class GdDriver extends AbstractDriver |
||||
| 30 | {
|
||||
| 31 | /** |
||||
| 32 | * GdDriver::__destruct |
||||
| 33 | */ |
||||
| 34 | public function __destruct() |
||||
| 35 | {
|
||||
| 36 | if (is_resource($this->sourceImageResource)) {
|
||||
| 37 | @imagedestroy($this->sourceImageResource); |
||||
|
0 ignored issues
–
show
|
|||||
| 38 | } |
||||
| 39 | |||||
| 40 | if (is_resource($this->resampleImageResource)) {
|
||||
| 41 | @imagedestroy($this->resampleImageResource); |
||||
| 42 | } |
||||
| 43 | } |
||||
| 44 | |||||
| 45 | // ------------------------------------------------------------------------ |
||||
| 46 | |||||
| 47 | /** |
||||
| 48 | * GdDriver::createFromString |
||||
| 49 | * |
||||
| 50 | * Create image resource from image string. |
||||
| 51 | * |
||||
| 52 | * @param string $imageString Image string. |
||||
| 53 | * |
||||
| 54 | * @return void |
||||
| 55 | */ |
||||
| 56 | public function createFromString($imageString) |
||||
| 57 | {
|
||||
| 58 | $this->sourceImageResource = imagecreatefromstring($imageString); |
||||
|
0 ignored issues
–
show
It seems like
imagecreatefromstring($imageString) can also be of type false. However, the property $sourceImageResource is declared as type Gmagick|Imagick|resource. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||||
| 59 | } |
||||
| 60 | |||||
| 61 | // ------------------------------------------------------------------------ |
||||
| 62 | |||||
| 63 | /** |
||||
| 64 | * GdDriver::rotate |
||||
| 65 | * |
||||
| 66 | * Rotate an image with a given angle. |
||||
| 67 | * |
||||
| 68 | * @param float $degrees Image rotation degrees. |
||||
| 69 | * |
||||
| 70 | * @return void |
||||
| 71 | */ |
||||
| 72 | public function rotate($degrees) |
||||
| 73 | {
|
||||
| 74 | $resampleImageResource =& $this->getResampleImageResource(); |
||||
| 75 | |||||
| 76 | // Set the background color |
||||
| 77 | // This won't work with transparent PNG files so we are |
||||
| 78 | // going to have to figure out how to determine the color |
||||
| 79 | // of the alpha channel in a future release. |
||||
| 80 | $alphaChannel = imagecolorallocate($resampleImageResource, 255, 255, 255); |
||||
| 81 | |||||
| 82 | // Rotate it! |
||||
| 83 | $this->resampleImageResource = imagerotate($resampleImageResource, $degrees, $alphaChannel); |
||||
|
0 ignored issues
–
show
It seems like
imagerotate($resampleIma...degrees, $alphaChannel) can also be of type false. However, the property $resampleImageResource is declared as type Gmagick|Imagick|resource. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||||
| 84 | } |
||||
| 85 | |||||
| 86 | // ------------------------------------------------------------------------ |
||||
| 87 | |||||
| 88 | /** |
||||
| 89 | * GdDriver::flip |
||||
| 90 | * |
||||
| 91 | * Flip an image with a given axis. |
||||
| 92 | * |
||||
| 93 | * @param int $axis Flip axis. |
||||
| 94 | * |
||||
| 95 | * @return void |
||||
| 96 | */ |
||||
| 97 | public function flip($axis) |
||||
| 98 | {
|
||||
| 99 | $gdAxis = [ |
||||
| 100 | 1 => IMG_FLIP_HORIZONTAL, |
||||
| 101 | 2 => IMG_FLIP_VERTICAL, |
||||
| 102 | 3 => IMG_FLIP_BOTH, |
||||
| 103 | ]; |
||||
| 104 | |||||
| 105 | if (array_key_exists($axis, $gdAxis)) {
|
||||
| 106 | $resampleImageResource =& $this->getResampleImageResource(); |
||||
| 107 | imageflip($resampleImageResource, $axis); |
||||
| 108 | } |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | // ------------------------------------------------------------------------ |
||||
| 112 | |||||
| 113 | /** |
||||
| 114 | * GdDriver::resize |
||||
| 115 | * |
||||
| 116 | * Resize an image using the given new width and height. |
||||
| 117 | * |
||||
| 118 | * @param bool $crop Perform auto crop or not |
||||
| 119 | * |
||||
| 120 | * @return bool |
||||
| 121 | */ |
||||
| 122 | public function resize($crop = false) |
||||
| 123 | {
|
||||
| 124 | if ($crop) {
|
||||
| 125 | return $this->resizeCrop(); |
||||
| 126 | } else {
|
||||
| 127 | $sourceDimension = $this->sourceImageFile->getDimension(); |
||||
| 128 | $resampleDimension = $this->resampleImageFile->getDimension(); |
||||
| 129 | |||||
| 130 | if (($sourceDimension->getWidth() <= $resampleDimension->getWidth()) && ($sourceDimension->getHeight() <= $resampleDimension->getHeight())) {
|
||||
| 131 | return true; |
||||
| 132 | } //no resizing needed |
||||
| 133 | |||||
| 134 | //try max width first... |
||||
| 135 | $resizeRatio = $resampleDimension->getWidth() / $sourceDimension->getWidth(); |
||||
| 136 | $resizeWidth = $resampleDimension->getWidth(); |
||||
| 137 | $resizeHeight = $sourceDimension->getHeight() * $resizeRatio; |
||||
| 138 | |||||
| 139 | //if that didn't work |
||||
| 140 | if ($resizeHeight > $resampleDimension->getHeight()) {
|
||||
| 141 | $resizeRatio = $resampleDimension->getHeight() / $sourceDimension->getHeight(); |
||||
| 142 | $resizeHeight = $resampleDimension->getHeight(); |
||||
| 143 | $resizeWidth = $sourceDimension->getWidth() * $resizeRatio; |
||||
| 144 | } |
||||
| 145 | |||||
| 146 | if (function_exists('imagecreatetruecolor')) {
|
||||
| 147 | $this->resampleImageResource = imagecreatetruecolor($resizeWidth, $resizeHeight); |
||||
|
0 ignored issues
–
show
It seems like
imagecreatetruecolor($resizeWidth, $resizeHeight) can also be of type false. However, the property $resampleImageResource is declared as type Gmagick|Imagick|resource. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||||
| 148 | |||||
| 149 | imagealphablending($this->resampleImageResource, false); |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $image of imagealphablending() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 150 | imagesavealpha($this->resampleImageResource, true); |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $image of imagesavealpha() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 151 | |||||
| 152 | $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127); |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $image of imagecolorallocatealpha() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 153 | imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(), |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $image of imagefilledrectangle() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 154 | $resampleDimension->getHeight(), $transparent); |
||||
| 155 | |||||
| 156 | return imagecopyresampled( |
||||
| 157 | $this->resampleImageResource, |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $dst_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 158 | $this->sourceImageResource, |
||||
|
0 ignored issues
–
show
It seems like
$this->sourceImageResource can also be of type Gmagick and Imagick; however, parameter $src_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 159 | 0, |
||||
| 160 | 0, |
||||
| 161 | 0, |
||||
| 162 | 0, |
||||
| 163 | $resizeWidth, |
||||
| 164 | $resizeHeight, |
||||
| 165 | $sourceDimension->getWidth(), |
||||
| 166 | $sourceDimension->getHeight() |
||||
| 167 | ); |
||||
| 168 | } else {
|
||||
| 169 | $this->resampleImageResource = imagecreate($resampleDimension->getWidth(), |
||||
| 170 | $resampleDimension->getHeight()); |
||||
| 171 | |||||
| 172 | imagealphablending($this->resampleImageResource, false); |
||||
| 173 | imagesavealpha($this->resampleImageResource, true); |
||||
| 174 | |||||
| 175 | $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127); |
||||
| 176 | imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(), |
||||
| 177 | $resampleDimension->getHeight(), $transparent); |
||||
| 178 | |||||
| 179 | return imagecopyresampled( |
||||
| 180 | $this->resampleImageResource, |
||||
| 181 | $this->sourceImageResource, |
||||
| 182 | 0, |
||||
| 183 | 0, |
||||
| 184 | 0, |
||||
| 185 | 0, |
||||
| 186 | $resizeWidth, |
||||
| 187 | $resizeHeight, |
||||
| 188 | $sourceDimension->getWidth(), |
||||
| 189 | $sourceDimension->getHeight() |
||||
| 190 | ); |
||||
| 191 | } |
||||
| 192 | } |
||||
| 193 | } |
||||
| 194 | |||||
| 195 | // ------------------------------------------------------------------------ |
||||
| 196 | |||||
| 197 | public function resizeCrop() |
||||
| 198 | {
|
||||
| 199 | $sourceDimension = $this->sourceImageFile->getDimension(); |
||||
| 200 | $resampleDimension = $this->resampleImageFile->getDimension(); |
||||
| 201 | |||||
| 202 | if ($resampleDimension->getOrientation() === 'SQUARE') {
|
||||
| 203 | if ($sourceDimension->getOrientation() === 'LANDSCAPE') {
|
||||
| 204 | $sourceSquareSize = $sourceDimension->getHeight(); |
||||
| 205 | |||||
| 206 | $sourceDimension = new Dimension( |
||||
| 207 | $sourceSquareSize, |
||||
| 208 | $sourceSquareSize, |
||||
| 209 | ($sourceDimension->getWidth() - $sourceDimension->getHeight()) / 2, |
||||
| 210 | 0 |
||||
| 211 | ); |
||||
| 212 | } elseif ($sourceDimension->getOrientation() === 'PORTRAIT') {
|
||||
| 213 | $sourceSquareSize = $sourceDimension->getWidth(); |
||||
| 214 | |||||
| 215 | $sourceDimension = new Dimension( |
||||
| 216 | $sourceSquareSize, |
||||
| 217 | $sourceSquareSize, |
||||
| 218 | 0, |
||||
| 219 | ($sourceDimension->getHeight() - $sourceDimension->getWidth()) / 2 |
||||
| 220 | ); |
||||
| 221 | } |
||||
| 222 | } else {
|
||||
| 223 | |||||
| 224 | //try max width first... |
||||
| 225 | $resizeRatio = $resampleDimension->getWidth() / $sourceDimension->getWidth(); |
||||
| 226 | $resizeWidth = $resampleDimension->getWidth(); |
||||
| 227 | $resizeHeight = $sourceDimension->getHeight() * $resizeRatio; |
||||
| 228 | |||||
| 229 | //if that didn't work |
||||
| 230 | if ($resizeHeight > $resampleDimension->getHeight()) {
|
||||
| 231 | $resizeRatio = $resampleDimension->getHeight() / $sourceDimension->getHeight(); |
||||
| 232 | $resizeHeight = $resampleDimension->getHeight(); |
||||
| 233 | $resizeWidth = $sourceDimension->getWidth() * $resizeRatio; |
||||
| 234 | } |
||||
| 235 | |||||
| 236 | switch ($resampleDimension->getFocus()) {
|
||||
| 237 | default: |
||||
| 238 | case 'CENTER': |
||||
| 239 | |||||
| 240 | $sourceDimension = new Dimension( |
||||
| 241 | $resizeWidth, |
||||
| 242 | $resizeHeight, |
||||
| 243 | ($sourceDimension->getWidth() / 2) - ($resizeWidth / 2), |
||||
| 244 | ($sourceDimension->getHeight() / 2) - ($resizeHeight / 2) |
||||
| 245 | ); |
||||
| 246 | |||||
| 247 | break; |
||||
| 248 | case 'NORTH': |
||||
| 249 | $sourceDimension = new Dimension( |
||||
| 250 | $resizeWidth, |
||||
| 251 | $resizeHeight, |
||||
| 252 | ($sourceDimension->getWidth() - $resizeWidth) / 2, |
||||
| 253 | 0 |
||||
| 254 | ); |
||||
| 255 | break; |
||||
| 256 | case 'NORTHWEST': |
||||
| 257 | $sourceDimension = new Dimension( |
||||
| 258 | $resizeWidth, |
||||
| 259 | $resizeHeight, |
||||
| 260 | 0, |
||||
| 261 | 0 |
||||
| 262 | ); |
||||
| 263 | break; |
||||
| 264 | case 'NORTHEAST': |
||||
| 265 | $sourceDimension = new Dimension( |
||||
| 266 | $resizeWidth, |
||||
| 267 | $resizeHeight, |
||||
| 268 | $sourceDimension->getWidth() - $resizeWidth, |
||||
| 269 | 0 |
||||
| 270 | ); |
||||
| 271 | break; |
||||
| 272 | case 'SOUTH': |
||||
| 273 | $sourceDimension = new Dimension( |
||||
| 274 | $resizeWidth, |
||||
| 275 | $resizeHeight, |
||||
| 276 | ($sourceDimension->getWidth() - $resizeWidth) / 2, |
||||
| 277 | $sourceDimension->getHeight() - $resizeHeight |
||||
| 278 | ); |
||||
| 279 | break; |
||||
| 280 | case 'SOUTHWEST': |
||||
| 281 | $sourceDimension = new Dimension( |
||||
| 282 | $resizeWidth, |
||||
| 283 | $resizeHeight, |
||||
| 284 | 0, |
||||
| 285 | $sourceDimension->getHeight() - $resizeHeight |
||||
| 286 | ); |
||||
| 287 | break; |
||||
| 288 | case 'SOUTHEAST': |
||||
| 289 | $sourceDimension = new Dimension( |
||||
| 290 | $resizeWidth, |
||||
| 291 | $resizeHeight, |
||||
| 292 | $sourceDimension->getWidth() - $resizeWidth, |
||||
| 293 | $sourceDimension->getHeight() - $resizeHeight |
||||
| 294 | ); |
||||
| 295 | break; |
||||
| 296 | case 'WEST': |
||||
| 297 | $sourceDimension = new Dimension( |
||||
| 298 | $resizeWidth, |
||||
| 299 | $resizeHeight, |
||||
| 300 | 0, |
||||
| 301 | ($sourceDimension->getHeight() - $resizeHeight) / 2 |
||||
| 302 | ); |
||||
| 303 | break; |
||||
| 304 | case 'EAST': |
||||
| 305 | $sourceDimension = new Dimension( |
||||
| 306 | $resizeWidth, |
||||
| 307 | $resizeHeight, |
||||
| 308 | $sourceDimension->getWidth() - $resizeWidth, |
||||
| 309 | ($sourceDimension->getHeight() - $resizeHeight) / 2 |
||||
| 310 | ); |
||||
| 311 | break; |
||||
| 312 | } |
||||
| 313 | } |
||||
| 314 | |||||
| 315 | $resampleAxis = $this->resampleImageFile->getDimension()->getAxis(); |
||||
| 316 | $sourceAxis = $sourceDimension->getAxis(); |
||||
| 317 | |||||
| 318 | if (function_exists('imagecreatetruecolor')) {
|
||||
| 319 | $this->resampleImageResource = imagecreatetruecolor( |
||||
|
0 ignored issues
–
show
It seems like
imagecreatetruecolor($re...Dimension->getHeight()) can also be of type false. However, the property $resampleImageResource is declared as type Gmagick|Imagick|resource. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||||
| 320 | $resampleDimension->getWidth(), |
||||
| 321 | $resampleDimension->getHeight() |
||||
| 322 | ); |
||||
| 323 | |||||
| 324 | imagealphablending($this->resampleImageResource, false); |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $image of imagealphablending() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 325 | imagesavealpha($this->resampleImageResource, true); |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $image of imagesavealpha() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 326 | |||||
| 327 | $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127); |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $image of imagecolorallocatealpha() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 328 | imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(), |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $image of imagefilledrectangle() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 329 | $resampleDimension->getHeight(), $transparent); |
||||
| 330 | |||||
| 331 | return imagecopyresampled( |
||||
| 332 | $this->resampleImageResource, |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $dst_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 333 | $this->sourceImageResource, |
||||
|
0 ignored issues
–
show
It seems like
$this->sourceImageResource can also be of type Gmagick and Imagick; however, parameter $src_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 334 | $resampleAxis->getX(), |
||||
| 335 | $resampleAxis->getY(), |
||||
| 336 | $sourceAxis->getX(), |
||||
| 337 | $sourceAxis->getY(), |
||||
| 338 | $resampleDimension->getWidth(), |
||||
| 339 | $resampleDimension->getHeight(), |
||||
| 340 | $sourceDimension->getWidth(), |
||||
| 341 | $sourceDimension->getHeight() |
||||
| 342 | ); |
||||
| 343 | } else {
|
||||
| 344 | $this->resampleImageResource = imagecreate($resampleDimension->getWidth(), |
||||
| 345 | $resampleDimension->getHeight()); |
||||
| 346 | |||||
| 347 | imagealphablending($this->resampleImageResource, false); |
||||
| 348 | imagesavealpha($this->resampleImageResource, true); |
||||
| 349 | |||||
| 350 | $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127); |
||||
| 351 | imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(), |
||||
| 352 | $resampleDimension->getHeight(), $transparent); |
||||
| 353 | |||||
| 354 | return imagecopyresized( |
||||
| 355 | $this->resampleImageResource, |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $dst_image of imagecopyresized() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 356 | $this->sourceImageResource, |
||||
|
0 ignored issues
–
show
It seems like
$this->sourceImageResource can also be of type Gmagick and Imagick; however, parameter $src_image of imagecopyresized() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 357 | $resampleAxis->getX(), |
||||
| 358 | $resampleAxis->getY(), |
||||
| 359 | $sourceAxis->getX(), |
||||
| 360 | $sourceAxis->getY(), |
||||
| 361 | $resampleDimension->getWidth(), |
||||
| 362 | $resampleDimension->getHeight(), |
||||
| 363 | $sourceDimension->getWidth(), |
||||
| 364 | $sourceDimension->getHeight() |
||||
| 365 | ); |
||||
| 366 | } |
||||
| 367 | } |
||||
| 368 | |||||
| 369 | // ------------------------------------------------------------------------ |
||||
| 370 | |||||
| 371 | /** |
||||
| 372 | * GdDriver::crop |
||||
| 373 | * |
||||
| 374 | * Crop an image. |
||||
| 375 | * |
||||
| 376 | * @param \O2System\Image\Dimension $dimension |
||||
| 377 | * |
||||
| 378 | * @return bool |
||||
| 379 | */ |
||||
| 380 | public function crop(Dimension $dimension) |
||||
| 381 | {
|
||||
| 382 | $resampleImageResource =& $this->getResampleImageResource(); |
||||
| 383 | |||||
| 384 | if (false !== ($resampleCropImage = imagecrop($resampleImageResource, [ |
||||
| 385 | 'x' => $dimension->getAxis()->getX(), |
||||
| 386 | 'y' => $dimension->getAxis()->getY(), |
||||
| 387 | 'width' => $dimension->getWidth(), |
||||
| 388 | 'height' => $dimension->getHeight(), |
||||
| 389 | ])) |
||||
| 390 | ) {
|
||||
| 391 | $resampleImageResource = $resampleCropImage; |
||||
| 392 | |||||
| 393 | return true; |
||||
| 394 | } |
||||
| 395 | |||||
| 396 | return false; |
||||
| 397 | } |
||||
| 398 | |||||
| 399 | /** |
||||
| 400 | * GdDriver::watermark |
||||
| 401 | * |
||||
| 402 | * Watermark an image. |
||||
| 403 | * |
||||
| 404 | * @param \O2System\Image\Abstracts\AbstractWatermark $watermark |
||||
| 405 | * |
||||
| 406 | * @return bool |
||||
| 407 | */ |
||||
| 408 | public function watermark(AbstractWatermark $watermark) |
||||
| 409 | {
|
||||
| 410 | $resampleImageResource =& $this->getResampleImageResource(); |
||||
| 411 | |||||
| 412 | if ($watermark instanceof Text) {
|
||||
| 413 | $textBox = imagettfbbox($watermark->getFontSize(), $watermark->getAngle(), $watermark->getFontPath(), |
||||
| 414 | $watermark->getString()); |
||||
| 415 | |||||
| 416 | if ($textBox[ 0 ] < 0 and $textBox[ 6 ]) {
|
||||
| 417 | $textBox[ 1 ] += $textBox[ 0 ]; |
||||
| 418 | $textBox[ 3 ] += $textBox[ 0 ]; |
||||
| 419 | $textBox[ 5 ] += $textBox[ 0 ]; |
||||
| 420 | $textBox[ 7 ] += $textBox[ 0 ]; |
||||
| 421 | } |
||||
| 422 | |||||
| 423 | $textBox = array_map('abs', $textBox);
|
||||
|
0 ignored issues
–
show
It seems like
$textBox can also be of type false; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 424 | |||||
| 425 | $watermarkImageWidth = max($textBox[ 0 ], $textBox[ 2 ], $textBox[ 4 ], $textBox[ 6 ]); |
||||
| 426 | $watermarkImageHeight = max($textBox[ 1 ], $textBox[ 3 ], $textBox[ 5 ], $textBox[ 7 ]); |
||||
| 427 | |||||
| 428 | if (false !== ($watermarkAxis = $watermark->getAxis())) {
|
||||
|
0 ignored issues
–
show
|
|||||
| 429 | $watermarkImageAxisX = $watermarkAxis->getX(); |
||||
| 430 | $watermarkImageAxisY = $watermarkAxis->getY(); |
||||
| 431 | } else {
|
||||
| 432 | switch ($watermark->getPosition()) {
|
||||
| 433 | default: |
||||
| 434 | case 'MIDDLE_MIDDLE': |
||||
| 435 | case 'MIDDLE': |
||||
| 436 | case 'CENTER': |
||||
| 437 | $watermarkImageAxisX = (imagesx($resampleImageResource) - $watermarkImageWidth) / 2; |
||||
| 438 | $watermarkImageAxisY = (imagesy($resampleImageResource) - $watermarkImageHeight) / 2; |
||||
| 439 | break; |
||||
| 440 | |||||
| 441 | case 'MIDDLE_LEFT': |
||||
| 442 | $watermarkImageAxisX = $watermark->getPadding(); |
||||
| 443 | $watermarkImageAxisY = (imagesy($resampleImageResource) - $watermarkImageHeight) / 2; |
||||
| 444 | break; |
||||
| 445 | |||||
| 446 | case 'MIDDLE_RIGHT': |
||||
| 447 | $watermarkImageAxisX = imagesx($resampleImageResource) - ($watermarkImageWidth + $watermark->getPadding()); |
||||
| 448 | $watermarkImageAxisY = (imagesy($resampleImageResource) - $watermarkImageHeight) / 2; |
||||
| 449 | break; |
||||
| 450 | |||||
| 451 | case 'MIDDLE_TOP': |
||||
| 452 | $watermarkImageAxisX = (imagesx($resampleImageResource) - $watermarkImageWidth) / 2; |
||||
| 453 | $watermarkImageAxisY = $watermarkImageHeight + $watermark->getPadding(); |
||||
| 454 | break; |
||||
| 455 | |||||
| 456 | case 'MIDDLE_BOTTOM': |
||||
| 457 | $watermarkImageAxisX = (imagesx($resampleImageResource) - $watermarkImageWidth) / 2; |
||||
| 458 | $watermarkImageAxisY = imagesy($resampleImageResource) - ($watermarkImageHeight + $watermark->getPadding()); |
||||
| 459 | break; |
||||
| 460 | |||||
| 461 | case 'TOP_LEFT': |
||||
| 462 | $watermarkImageAxisX = $watermark->getPadding(); |
||||
| 463 | $watermarkImageAxisY = $watermarkImageHeight + $watermark->getPadding(); |
||||
| 464 | break; |
||||
| 465 | |||||
| 466 | case 'TOP_RIGHT': |
||||
| 467 | $watermarkImageAxisX = imagesx($resampleImageResource) - ($watermarkImageWidth + $watermark->getPadding()); |
||||
| 468 | $watermarkImageAxisY = $watermarkImageHeight + $watermark->getPadding(); |
||||
| 469 | break; |
||||
| 470 | |||||
| 471 | case 'BOTTOM_LEFT': |
||||
| 472 | $watermarkImageAxisX = $watermark->getPadding(); |
||||
| 473 | $watermarkImageAxisY = imagesy($resampleImageResource) - $watermarkImageHeight + $watermark->getPadding(); |
||||
| 474 | break; |
||||
| 475 | |||||
| 476 | case 'BOTTOM_RIGHT': |
||||
| 477 | $watermarkImageAxisX = imagesx($resampleImageResource) - ($watermarkImageWidth + $watermark->getPadding()); |
||||
| 478 | $watermarkImageAxisY = imagesy($resampleImageResource) - ($watermarkImageHeight + $watermark->getPadding()); |
||||
| 479 | break; |
||||
| 480 | } |
||||
| 481 | } |
||||
| 482 | |||||
| 483 | /* Set RGB values for text |
||||
| 484 | * |
||||
| 485 | * First character is #, so we don't really need it. |
||||
| 486 | * Get the rest of the string and split it into 2-length |
||||
| 487 | * hex values: |
||||
| 488 | */ |
||||
| 489 | $textColor = str_split(substr($watermark->getFontColor(), 1, 6), 2); |
||||
| 490 | $textColor = imagecolorclosest($resampleImageResource, hexdec($textColor[ 0 ]), |
||||
|
0 ignored issues
–
show
It seems like
hexdec($textColor[0]) can also be of type double; however, parameter $red of imagecolorclosest() does only seem to accept integer, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 491 | hexdec($textColor[ 1 ]), |
||||
|
0 ignored issues
–
show
It seems like
hexdec($textColor[1]) can also be of type double; however, parameter $green of imagecolorclosest() does only seem to accept integer, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 492 | hexdec($textColor[ 2 ])); |
||||
|
0 ignored issues
–
show
It seems like
hexdec($textColor[2]) can also be of type double; however, parameter $blue of imagecolorclosest() does only seem to accept integer, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 493 | |||||
| 494 | imagettftext( |
||||
| 495 | $resampleImageResource, |
||||
| 496 | $watermark->getFontSize(), |
||||
| 497 | $watermark->getAngle(), |
||||
| 498 | $watermarkImageAxisX, |
||||
| 499 | $watermarkImageAxisY, |
||||
| 500 | $textColor, |
||||
| 501 | $watermark->getFontPath(), |
||||
| 502 | $watermark->getString() |
||||
| 503 | ); |
||||
| 504 | |||||
| 505 | return true; |
||||
| 506 | } elseif ($watermark instanceof Overlay) {
|
||||
| 507 | |||||
| 508 | $watermarkImage = new self; |
||||
| 509 | $watermarkImage->setSourceImage($watermark->getImagePath()); |
||||
| 510 | $watermarkImage->createFromSource(); |
||||
| 511 | |||||
| 512 | $watermarkImageFile = $watermarkImage->getSourceImageFile(); |
||||
| 513 | $watermarkImageDimension = $watermarkImageFile->getDimension(); |
||||
| 514 | $watermarkImageDimension->maintainAspectRatio = true; |
||||
| 515 | |||||
| 516 | if (false === ($scale = $watermark->getImageScale())) {
|
||||
|
0 ignored issues
–
show
|
|||||
| 517 | $scale = min( |
||||
| 518 | round(((imagesx($resampleImageResource) / 2) / $watermarkImageDimension->getWidth()) * 100), |
||||
| 519 | round(((imagesy($resampleImageResource) / 2) / $watermarkImageDimension->getHeight()) * 100) |
||||
| 520 | ); |
||||
| 521 | } |
||||
| 522 | |||||
| 523 | if ($scale > 0) {
|
||||
| 524 | $watermarkImage->setResampleImage($watermarkImageFile->withDimension( |
||||
| 525 | $watermarkImageDimension |
||||
| 526 | ->withScale($scale) |
||||
| 527 | )); |
||||
| 528 | } |
||||
| 529 | |||||
| 530 | if ($watermarkImage->scale()) {
|
||||
| 531 | $watermarkImageResource = $watermarkImage->getResampleImageResource(); |
||||
| 532 | |||||
| 533 | $watermarkImageWidth = imagesx($watermarkImageResource); |
||||
| 534 | $watermarkImageHeight = imagesy($watermarkImageResource); |
||||
| 535 | |||||
| 536 | if (false !== ($watermarkAxis = $watermark->getAxis())) {
|
||||
|
0 ignored issues
–
show
|
|||||
| 537 | $watermarkImageAxisX = $watermarkAxis->getX(); |
||||
| 538 | $watermarkImageAxisY = $watermarkAxis->getY(); |
||||
| 539 | } else {
|
||||
| 540 | switch ($watermark->getPosition()) {
|
||||
| 541 | default: |
||||
| 542 | case 'MIDDLE_MIDDLE': |
||||
| 543 | case 'MIDDLE': |
||||
| 544 | case 'CENTER': |
||||
| 545 | $watermarkImageAxisX = (imagesx($resampleImageResource) - $watermarkImageWidth) / 2; |
||||
| 546 | $watermarkImageAxisY = (imagesy($resampleImageResource) - $watermarkImageHeight) / 2; |
||||
| 547 | break; |
||||
| 548 | |||||
| 549 | case 'MIDDLE_LEFT': |
||||
| 550 | $watermarkImageAxisX = $watermark->getPadding(); |
||||
| 551 | $watermarkImageAxisY = (imagesy($resampleImageResource) - $watermarkImageHeight) / 2; |
||||
| 552 | break; |
||||
| 553 | |||||
| 554 | case 'MIDDLE_RIGHT': |
||||
| 555 | $watermarkImageAxisX = imagesx($resampleImageResource) - ($watermarkImageWidth + $watermark->getPadding()); |
||||
| 556 | $watermarkImageAxisY = (imagesy($resampleImageResource) - $watermarkImageHeight) / 2; |
||||
| 557 | break; |
||||
| 558 | |||||
| 559 | case 'MIDDLE_TOP': |
||||
| 560 | $watermarkImageAxisX = (imagesx($resampleImageResource) - $watermarkImageWidth) / 2; |
||||
| 561 | $watermarkImageAxisY = $watermarkImageHeight + $watermark->getPadding(); |
||||
| 562 | break; |
||||
| 563 | |||||
| 564 | case 'MIDDLE_BOTTOM': |
||||
| 565 | $watermarkImageAxisX = (imagesx($resampleImageResource) - $watermarkImageWidth) / 2; |
||||
| 566 | $watermarkImageAxisY = imagesy($resampleImageResource) - ($watermarkImageHeight + $watermark->getPadding()); |
||||
| 567 | break; |
||||
| 568 | |||||
| 569 | case 'TOP_LEFT': |
||||
| 570 | $watermarkImageAxisX = $watermark->getPadding(); |
||||
| 571 | $watermarkImageAxisY = $watermarkImageHeight + $watermark->getPadding(); |
||||
| 572 | break; |
||||
| 573 | |||||
| 574 | case 'TOP_RIGHT': |
||||
| 575 | $watermarkImageAxisX = imagesx($resampleImageResource) - ($watermarkImageWidth + $watermark->getPadding()); |
||||
| 576 | $watermarkImageAxisY = $watermarkImageHeight + $watermark->getPadding(); |
||||
| 577 | break; |
||||
| 578 | |||||
| 579 | case 'BOTTOM_LEFT': |
||||
| 580 | $watermarkImageAxisX = $watermark->getPadding(); |
||||
| 581 | $watermarkImageAxisY = imagesy($resampleImageResource) - $watermarkImageHeight + $watermark->getPadding(); |
||||
| 582 | break; |
||||
| 583 | |||||
| 584 | case 'BOTTOM_RIGHT': |
||||
| 585 | $watermarkImageAxisX = imagesx($resampleImageResource) - ($watermarkImageWidth + $watermark->getPadding()); |
||||
| 586 | $watermarkImageAxisY = imagesy($resampleImageResource) - ($watermarkImageHeight + $watermark->getPadding()); |
||||
| 587 | break; |
||||
| 588 | } |
||||
| 589 | } |
||||
| 590 | |||||
| 591 | return imagecopy( |
||||
| 592 | $resampleImageResource, |
||||
| 593 | $watermarkImageResource, |
||||
| 594 | $watermarkImageAxisX, |
||||
| 595 | $watermarkImageAxisY, |
||||
| 596 | 0, |
||||
| 597 | 0, |
||||
| 598 | imagesx($watermarkImageResource), |
||||
| 599 | imagesy($watermarkImageResource) |
||||
| 600 | ); |
||||
| 601 | } |
||||
| 602 | } |
||||
| 603 | |||||
| 604 | return false; |
||||
| 605 | } |
||||
| 606 | |||||
| 607 | // ------------------------------------------------------------------------ |
||||
| 608 | |||||
| 609 | /** |
||||
| 610 | * GdDriver::createFromSource |
||||
| 611 | * |
||||
| 612 | * Create an image resource from source file. |
||||
| 613 | * |
||||
| 614 | * @return void |
||||
| 615 | */ |
||||
| 616 | public function createFromSource() |
||||
| 617 | {
|
||||
| 618 | /** |
||||
| 619 | * A work-around for some improperly formatted, but |
||||
| 620 | * usable JPEGs; known to be produced by Samsung |
||||
| 621 | * smartphones' front-facing cameras. |
||||
| 622 | * |
||||
| 623 | * @see https://bugs.php.net/bug.php?id=72404 |
||||
| 624 | */ |
||||
| 625 | ini_set('gd.jpeg_ignore_warning', 1);
|
||||
| 626 | |||||
| 627 | $mime = $this->sourceImageFile->getMime(); |
||||
| 628 | $mime = is_array($mime) ? $mime[ 0 ] : $mime; |
||||
| 629 | |||||
| 630 | try {
|
||||
| 631 | switch ($mime) {
|
||||
| 632 | case 'image/jpg': |
||||
| 633 | case 'image/jpeg': |
||||
| 634 | $this->sourceImageResource = imagecreatefromjpeg($this->sourceImageFile->getRealPath()); |
||||
|
0 ignored issues
–
show
It seems like
imagecreatefromjpeg($thi...ageFile->getRealPath()) can also be of type false. However, the property $sourceImageResource is declared as type Gmagick|Imagick|resource. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||||
| 635 | break; |
||||
| 636 | |||||
| 637 | case 'image/gif': |
||||
| 638 | $this->sourceImageResource = imagecreatefromgif($this->sourceImageFile->getRealPath()); |
||||
| 639 | break; |
||||
| 640 | |||||
| 641 | case 'image/png': |
||||
| 642 | case 'image/x-png': |
||||
| 643 | $this->sourceImageResource = imagecreatefrompng($this->sourceImageFile->getRealPath()); |
||||
| 644 | break; |
||||
| 645 | } |
||||
| 646 | |||||
| 647 | // Convert pallete images to true color images |
||||
| 648 | imagepalettetotruecolor($this->sourceImageResource); |
||||
|
0 ignored issues
–
show
It seems like
$this->sourceImageResource can also be of type Gmagick and Imagick; however, parameter $image of imagepalettetotruecolor() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 649 | } catch (\Exception $e) {
|
||||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||||
| 650 | |||||
| 651 | } |
||||
| 652 | } |
||||
| 653 | |||||
| 654 | // ------------------------------------------------------------------------ |
||||
| 655 | |||||
| 656 | /** |
||||
| 657 | * GdDriver::scale |
||||
| 658 | * |
||||
| 659 | * Scale an image with a given scale. |
||||
| 660 | * |
||||
| 661 | * @return bool |
||||
| 662 | */ |
||||
| 663 | public function scale() |
||||
| 664 | {
|
||||
| 665 | $sourceDimension = $this->sourceImageFile->getDimension(); |
||||
| 666 | $resampleDimension = $this->resampleImageFile->getDimension(); |
||||
| 667 | |||||
| 668 | $resampleAxis = $this->resampleImageFile->getDimension()->getAxis(); |
||||
| 669 | $sourceAxis = $sourceDimension->getAxis(); |
||||
| 670 | |||||
| 671 | if (function_exists('imagecreatetruecolor')) {
|
||||
| 672 | $this->resampleImageResource = imagecreatetruecolor( |
||||
|
0 ignored issues
–
show
It seems like
imagecreatetruecolor($re...Dimension->getHeight()) can also be of type false. However, the property $resampleImageResource is declared as type Gmagick|Imagick|resource. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||||
| 673 | $resampleDimension->getWidth(), |
||||
| 674 | $resampleDimension->getHeight() |
||||
| 675 | ); |
||||
| 676 | |||||
| 677 | imagealphablending($this->resampleImageResource, false); |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $image of imagealphablending() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 678 | imagesavealpha($this->resampleImageResource, true); |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $image of imagesavealpha() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 679 | |||||
| 680 | $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127); |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $image of imagecolorallocatealpha() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 681 | imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(), |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $image of imagefilledrectangle() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 682 | $resampleDimension->getHeight(), $transparent); |
||||
| 683 | |||||
| 684 | return imagecopyresampled( |
||||
| 685 | $this->resampleImageResource, |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $dst_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 686 | $this->sourceImageResource, |
||||
|
0 ignored issues
–
show
It seems like
$this->sourceImageResource can also be of type Gmagick and Imagick; however, parameter $src_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 687 | $resampleAxis->getX(), |
||||
| 688 | $resampleAxis->getY(), |
||||
| 689 | $sourceAxis->getX(), |
||||
| 690 | $sourceAxis->getY(), |
||||
| 691 | $resampleDimension->getWidth(), |
||||
| 692 | $resampleDimension->getHeight(), |
||||
| 693 | $sourceDimension->getWidth(), |
||||
| 694 | $sourceDimension->getHeight() |
||||
| 695 | ); |
||||
| 696 | } else {
|
||||
| 697 | $this->resampleImageResource = imagecreate($resampleDimension->getWidth(), |
||||
| 698 | $resampleDimension->getHeight()); |
||||
| 699 | |||||
| 700 | imagealphablending($this->resampleImageResource, false); |
||||
| 701 | imagesavealpha($this->resampleImageResource, true); |
||||
| 702 | |||||
| 703 | $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127); |
||||
| 704 | imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(), |
||||
| 705 | $resampleDimension->getHeight(), $transparent); |
||||
| 706 | |||||
| 707 | return imagecopyresized( |
||||
| 708 | $this->resampleImageResource, |
||||
|
0 ignored issues
–
show
It seems like
$this->resampleImageResource can also be of type false; however, parameter $dst_image of imagecopyresized() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 709 | $this->sourceImageResource, |
||||
|
0 ignored issues
–
show
It seems like
$this->sourceImageResource can also be of type Gmagick and Imagick; however, parameter $src_image of imagecopyresized() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 710 | $resampleAxis->getX(), |
||||
| 711 | $resampleAxis->getY(), |
||||
| 712 | $sourceAxis->getX(), |
||||
| 713 | $sourceAxis->getY(), |
||||
| 714 | $resampleDimension->getWidth(), |
||||
| 715 | $resampleDimension->getHeight(), |
||||
| 716 | $sourceDimension->getWidth(), |
||||
| 717 | $sourceDimension->getHeight() |
||||
| 718 | ); |
||||
| 719 | } |
||||
| 720 | } |
||||
| 721 | |||||
| 722 | // ------------------------------------------------------------------------ |
||||
| 723 | |||||
| 724 | /** |
||||
| 725 | * GdDriver::display |
||||
| 726 | * |
||||
| 727 | * Display an image. |
||||
| 728 | * |
||||
| 729 | * @return void |
||||
| 730 | */ |
||||
| 731 | public function display($quality = 100, $mime = null) |
||||
| 732 | {
|
||||
| 733 | $filename = pathinfo($this->sourceImageFile->getBasename(), PATHINFO_FILENAME); |
||||
| 734 | $extension = pathinfo($this->sourceImageFile->getBasename(), PATHINFO_EXTENSION); |
||||
| 735 | |||||
| 736 | if (empty($mime)) {
|
||||
| 737 | $mime = $this->sourceImageFile->getMime(); |
||||
| 738 | $mime = is_array($mime) ? $mime[ 0 ] : $mime; |
||||
| 739 | |||||
| 740 | $extensions = [ |
||||
| 741 | 'image/gif' => 'gif', |
||||
| 742 | 'image/jpg' => 'jpg', |
||||
| 743 | 'image/jpeg' => 'jpeg', |
||||
| 744 | 'image/png' => 'png', |
||||
| 745 | 'image/webp' => 'webp', |
||||
| 746 | ]; |
||||
| 747 | |||||
| 748 | $extension = $extensions[ $mime ]; |
||||
| 749 | } |
||||
| 750 | |||||
| 751 | header('Content-Disposition: filename=' . $filename . '.' . $extension);
|
||||
| 752 | header('Content-Transfer-Encoding: binary');
|
||||
| 753 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
|
||||
| 754 | header('Content-Type: ' . $mime);
|
||||
| 755 | |||||
| 756 | $blob = $this->blob($quality, $mime); |
||||
| 757 | |||||
| 758 | header('ETag: ' . md5($blob));
|
||||
| 759 | |||||
| 760 | echo $blob; |
||||
| 761 | |||||
| 762 | exit(0); |
||||
|
0 ignored issues
–
show
|
|||||
| 763 | } |
||||
| 764 | |||||
| 765 | // ------------------------------------------------------------------------ |
||||
| 766 | |||||
| 767 | /** |
||||
| 768 | * GdDriver::blob |
||||
| 769 | * |
||||
| 770 | * Returns image as string blob. |
||||
| 771 | * |
||||
| 772 | * @return string |
||||
| 773 | */ |
||||
| 774 | public function blob($quality = 100, $mime = null) |
||||
| 775 | {
|
||||
| 776 | $imageBlob = ''; |
||||
| 777 | |||||
| 778 | $filename = pathinfo($this->sourceImageFile->getBasename(), PATHINFO_FILENAME); |
||||
| 779 | $extension = pathinfo($this->sourceImageFile->getBasename(), PATHINFO_EXTENSION); |
||||
| 780 | |||||
| 781 | if (empty($mime)) {
|
||||
| 782 | $mime = $this->sourceImageFile->getMime(); |
||||
| 783 | $mime = is_array($mime) ? $mime[ 0 ] : $mime; |
||||
| 784 | |||||
| 785 | $extension = $this->getMimeExtension($mime); |
||||
| 786 | } |
||||
| 787 | |||||
| 788 | if ($this->save($tempImageFilePath = rtrim(sys_get_temp_dir(), |
||||
| 789 | DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename . '.' . $extension, |
||||
|
0 ignored issues
–
show
Are you sure
$extension of type false|mixed|string can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 790 | $quality) |
||||
| 791 | ) {
|
||||
| 792 | $imageBlob = readfile($tempImageFilePath); |
||||
| 793 | unlink($tempImageFilePath); |
||||
| 794 | } |
||||
| 795 | |||||
| 796 | return $imageBlob; |
||||
| 797 | } |
||||
| 798 | |||||
| 799 | // ------------------------------------------------------------------------ |
||||
| 800 | |||||
| 801 | /** |
||||
| 802 | * GdDriver::save |
||||
| 803 | * |
||||
| 804 | * Save an image. |
||||
| 805 | * |
||||
| 806 | * @param string $imageTargetFilePath |
||||
| 807 | * @param int $quality |
||||
| 808 | * |
||||
| 809 | * @return bool |
||||
| 810 | */ |
||||
| 811 | public function save($imageTargetFilePath, $quality = 100) |
||||
| 812 | {
|
||||
| 813 | $resampleImageResource =& $this->getResampleImageResource(); |
||||
| 814 | |||||
| 815 | $extension = pathinfo($imageTargetFilePath, PATHINFO_EXTENSION); |
||||
| 816 | |||||
| 817 | switch ($extension) {
|
||||
| 818 | case 'jpg': |
||||
| 819 | case 'jpeg': |
||||
| 820 | return (bool)@imagejpeg($resampleImageResource, $imageTargetFilePath, $quality); |
||||
| 821 | break; |
||||
|
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other Loading history...
|
|||||
| 822 | |||||
| 823 | case 'gif': |
||||
| 824 | return (bool)@imagegif($resampleImageResource, $imageTargetFilePath); |
||||
| 825 | break; |
||||
| 826 | |||||
| 827 | case 'png': |
||||
| 828 | imagealphablending($resampleImageResource, false); |
||||
| 829 | imagesavealpha($resampleImageResource, true); |
||||
| 830 | |||||
| 831 | return (bool)@imagepng($resampleImageResource, $imageTargetFilePath); |
||||
| 832 | break; |
||||
| 833 | |||||
| 834 | case 'webp': |
||||
| 835 | return (bool)@imagewebp($resampleImageResource, $imageTargetFilePath); |
||||
| 836 | break; |
||||
| 837 | } |
||||
| 838 | |||||
| 839 | return false; |
||||
| 840 | } |
||||
| 841 | } |
If you suppress an error, we recommend checking for the error condition explicitly: