mirror of
https://github.com/Intervention/image.git
synced 2025-08-20 20:51:20 +02:00
Optimize code
This commit is contained in:
@@ -17,6 +17,12 @@ class Profile extends File implements ProfileInterface
|
|||||||
*/
|
*/
|
||||||
public static function fromPath(string $path): self
|
public static function fromPath(string $path): self
|
||||||
{
|
{
|
||||||
return new self(fopen($path, 'r'));
|
$pointer = fopen($path, 'r');
|
||||||
|
|
||||||
|
if ($pointer === false) {
|
||||||
|
throw new RuntimeException('Unable to read image profile from path.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return new self($pointer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -8,10 +8,12 @@ use GdImage;
|
|||||||
use Intervention\Image\Analyzers\PixelColorAnalyzer as GenericPixelColorAnalyzer;
|
use Intervention\Image\Analyzers\PixelColorAnalyzer as GenericPixelColorAnalyzer;
|
||||||
use Intervention\Image\Exceptions\ColorException;
|
use Intervention\Image\Exceptions\ColorException;
|
||||||
use Intervention\Image\Exceptions\GeometryException;
|
use Intervention\Image\Exceptions\GeometryException;
|
||||||
|
use Intervention\Image\Exceptions\RuntimeException;
|
||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
use Intervention\Image\Interfaces\ColorspaceInterface;
|
use Intervention\Image\Interfaces\ColorspaceInterface;
|
||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
use Intervention\Image\Interfaces\SpecializedInterface;
|
use Intervention\Image\Interfaces\SpecializedInterface;
|
||||||
|
use ValueError;
|
||||||
|
|
||||||
class PixelColorAnalyzer extends GenericPixelColorAnalyzer implements SpecializedInterface
|
class PixelColorAnalyzer extends GenericPixelColorAnalyzer implements SpecializedInterface
|
||||||
{
|
{
|
||||||
@@ -31,16 +33,19 @@ class PixelColorAnalyzer extends GenericPixelColorAnalyzer implements Specialize
|
|||||||
/**
|
/**
|
||||||
* @throws GeometryException
|
* @throws GeometryException
|
||||||
* @throws ColorException
|
* @throws ColorException
|
||||||
|
* @throws RuntimeException
|
||||||
*/
|
*/
|
||||||
protected function colorAt(ColorspaceInterface $colorspace, GdImage $gd): ColorInterface
|
protected function colorAt(ColorspaceInterface $colorspace, GdImage $gd): ColorInterface
|
||||||
{
|
{
|
||||||
$index = @imagecolorat($gd, $this->x, $this->y);
|
$index = @imagecolorat($gd, $this->x, $this->y);
|
||||||
|
|
||||||
if (!imageistruecolor($gd)) {
|
if ($index === false) {
|
||||||
$index = imagecolorsforindex($gd, $index);
|
throw new RuntimeException('Unable to read color at pixel ' . $this->x . ', ' . $this->y . '.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($index === false) {
|
try {
|
||||||
|
$index = imagecolorsforindex($gd, $index);
|
||||||
|
} catch (ValueError) {
|
||||||
throw new GeometryException(
|
throw new GeometryException(
|
||||||
'The specified position is not in the valid image area.'
|
'The specified position is not in the valid image area.'
|
||||||
);
|
);
|
||||||
|
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Intervention\Image\Drivers\Gd;
|
namespace Intervention\Image\Drivers\Gd;
|
||||||
|
|
||||||
use Intervention\Image\Drivers\AbstractFontProcessor;
|
use Intervention\Image\Drivers\AbstractFontProcessor;
|
||||||
|
use Intervention\Image\Exceptions\FontException;
|
||||||
use Intervention\Image\Geometry\Point;
|
use Intervention\Image\Geometry\Point;
|
||||||
use Intervention\Image\Geometry\Rectangle;
|
use Intervention\Image\Geometry\Rectangle;
|
||||||
use Intervention\Image\Interfaces\FontInterface;
|
use Intervention\Image\Interfaces\FontInterface;
|
||||||
@@ -44,6 +45,10 @@ class FontProcessor extends AbstractFontProcessor
|
|||||||
string: $text
|
string: $text
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if ($box === false) {
|
||||||
|
throw new FontException('Unable to calculate box size of font.');
|
||||||
|
}
|
||||||
|
|
||||||
// build size from points
|
// build size from points
|
||||||
return new Rectangle(
|
return new Rectangle(
|
||||||
width: intval(abs($box[6] - $box[4])), // difference of upper-left-x and upper-right-x
|
width: intval(abs($box[6] - $box[4])), // difference of upper-left-x and upper-right-x
|
||||||
|
@@ -22,6 +22,11 @@ trait CanBuildFilePointer
|
|||||||
is_resource($data) && get_resource_type($data) === 'stream' => fn(mixed $data) => $data,
|
is_resource($data) && get_resource_type($data) === 'stream' => fn(mixed $data) => $data,
|
||||||
is_string($data) => function (mixed $data) {
|
is_string($data) => function (mixed $data) {
|
||||||
$pointer = fopen('php://temp', 'r+');
|
$pointer = fopen('php://temp', 'r+');
|
||||||
|
|
||||||
|
if ($pointer === false) {
|
||||||
|
throw new RuntimeException('Unable to build file pointer.');
|
||||||
|
}
|
||||||
|
|
||||||
fwrite($pointer, $data);
|
fwrite($pointer, $data);
|
||||||
return $pointer;
|
return $pointer;
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user