我有这个类,它将给定图像的大小调整为设定大小,并且还可以在其上放置水印(图章)。
if let navigationController = self.window?.rootViewController as?
UINavigationController {
navigationController.popToRootViewControllerAnimated(true)
}
我的问题是在使用WaterMark()函数后,为什么 class Picture
{
private $src_path, $src_image, $width, $height, $resized_image;
public function __construct($src_path)
{
if (!file_exists($src_path)) {
throw new InvalidArgumentException('Invalid image path!');
}
$this->src_path = $src_path;
$this->loadImage();
}
private function loadImage()
{
if (exif_imagetype($this->src_path) == IMAGETYPE_JPEG) {
$this->src_image = imagecreatefromjpeg($this->src_path);
} elseif (exif_imagetype($this->src_path) == IMAGETYPE_PNG) {
$this->src_image = imagecreatefrompng($this->src_path);
} else {
throw new InvalidArgumentException('Your image type is not JPEG or PNG!');
}
}
public function waterMark()
{
$stamp = imagecreatefromjpeg('C:\Users\Public\Pictures\Sample Pictures\resized.jpg');
$im = $this->resized_image;
$marge_right = 10;
$marge_bottom = 80;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
}
public function resizeImage($w, $h)
{
list($width, $height) = getimagesize($this->src_path);
$ratio = $width / $height;
if ($w / $h > $ratio) {
$new_width = $h * $ratio;
$new_height = $h;
}else {
$new_height = $w / $ratio;
$new_width = $w;
}
$this->resized_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($this->resized_image, $this->src_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
}
public function saveImage($quality, $destination = null)
{
if (!$destination) {
$length = strlen($this->src_path) - strlen((string) $this->src_image);
$destination = substr($this->src_path, 0, $length-3).'resized.jpg';
}
return imagejpeg($this->resized_image, $destination, $quality);
}
}
$pic = new Picture('C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg');
$pic->resizeImage(400, 400);
$pic->saveImage(100, 'C:/Users/Public/Pictures/ResizedImage.jpg');
$pic->waterMark();
//Why is the original picture ($this->resized_image)
//is now watermarked ?
//Why is there a reference here ?
$pic->saveImage(100, 'C:/Users/Public/Pictures/WhyAmIWaterMarked.jpg');
变量会更改为WaterMarked变量的值?
$this->resized_image
我写了另一个类来测试这种行为:
$im = $this->resized_image;
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
但结果如预期: 15,15,15;
那么,如果变量(class TestReference
{
public $foo;
function setFoo($value)
{
$this->foo = $value;
}
function getFoo()
{
return $this->foo;
}
function getFooBar()
{
$tmp = $this->foo;
$tmp = 20;
return $this->foo;
}
}
$bar = new TestReference();
$bar->setFoo(15);
echo $bar->getFoo().'<br/>';
echo $bar->getFooBar().'<br/>';
echo $bar->getFoo().'<br/>';
)被临时变量覆盖,那么Picture类怎么来呢?