``
php,class ImageProcessor {, private $image;,, public function __construct($imagePath) {, $this>image = imagecreatefromjpeg($imagePath);, },, public function resize($width, $height) {, $newImage = imagecreatetruecolor($width, $height);, imagecopyresampled($newImage, $this>image, 0, 0, 0, 0, $width, $height, imagesx($this>image), imagesy($this>image));, return $newImage;, },},``
<?php
class ImageProcessor {
private $image;
private $width;
private $height;
public function __construct($filePath) {
if (!file_exists($filePath)) {
throw new Exception("File not found: " . $filePath);
}
$this>image = imagecreatefromjpeg($filePath);
$this>width = imagesx($this>image);
$this>height = imagesy($this>image);
}
public function resize($newWidth, $newHeight) {
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($resizedImage, $this>image, 0, 0, 0, 0, $newWidth, $newHeight, $this>width, $this>height);
$this>image = $resizedImage;
$this>width = $newWidth;
$this>height = $newHeight;
}
public function save($outputPath) {
imagejpeg($this>image, $outputPath);
}
public function getDimensions() {
return array('width' => $this>width, 'height' => $this>height);
}
}
// 使用示例
try {
$processor = new ImageProcessor('input.jpg');
$processor>resize(300, 200);
$processor>save('output.jpg');
echo "Image resized and saved successfully!";
} catch (Exception $e) {
echo "Error: " . $e>getMessage();
}
?>

(图片来源网络,侵删)