Вземане част от снимка

What

Registered
Примерно имам тази снимка
image-2.jpg


и искам да взема част от нея (и самата снимка трябва да си има x y)
37977900.jpg



как да стане. Няма значение дали с JS или PHP
 
пич не ми трябва аз да тегля чертата каде да се реже, а самия кода да го прави.
примерно съм му задал x и y къде да реже и то си го прави автоматично.
 
аз като видях че не ми дадоха това което търсех взех и се поразрових из image библиотеката на php и САМ си саставих тая функция :)

Код:
function cutimage($file,$outputx,$outputy,$beginx,$beginy,$endx,$endy){
	$infoPath = @pathinfo($file);
	header("Content-Type: image/".$infoPath["extension"]);
	if($infoPath["extension"]=="jpg")
		$src = imagecreatefromjpeg($file);
	elseif($infoPath["extension"]=="png")
		$src = imagecreatefrompng($file);
	elseif($infoPath["extension"]=="gif")
		$src = imagecreatefromgif($file);
		
	$dest = imagecreatetruecolor($outputx,$outputy);
	imagecopy($dest, $src,$beginx,$beginy,$endx,$endy,$outputx,$outputy);
	
	if($infoPath["extension"]=="jpg")
		imagejpeg($dest);
	elseif($infoPath["extension"]=="png")
		imagepng($dest);
	elseif($infoPath["extension"]=="gif")
		imagegif($dest);
	
	imagedestroy($dest);
	imagedestroy($src);
}
 
Туко-що ти написах един клас за това. Не знам дали има проблеми, но при моя тест не направи.

cropImage.php
<?php
class cropImage {
public function __construct($image, $width, $height, $x, $y, $newfile = "name.jpg") {
$this->image = $image;
$this->width = $width;
$this->height = $height;
$this->path = $path;
$this->x = $x;
$this->y = $y;
$this->newfile = $newfile;
$this->createMethod();
}

private function createMethod() {
$type = getimagesize($this->image);
switch($type[mime]) {
case 'image/jpeg':
$this->src = imagecreatefromjpeg($this->image);
break;
case 'image/gif':
$this->src = imagecreatefromgif($this->image);
break;
case 'image/png':
$this->src = imagecreatefrompng($this->image);
break;
}
}

public function crop() {
$this->dest = imagecreatetruecolor($this->width, $this->height);
imagecopy($this->dest, $this->src, 0, 0, $this->x, $this->y, $this->width, $this->height);
$this->path = "images/".$this->newfile;
$this->compress = 75;
imagejpeg($this->dest, $this->path,$this->compress);
imagedestroy($this->dest);
imagedestroy($this->src);
}

public function getImage() {
return $this->path;
}

}
?>

index.php
<?php
require_once("cropImage.php");
$dir = "images/"; //Име на папка с изображенията
$image = "file.png"; // Изображението;
$width = 200; //Широчината на новото изображение
$height = 200; // Височината на новото изображение
$x = 1;
$y = 1; // x и y координати на горния ляв ъгъл, от която да почне селекцията
$newfile = "newfile.jpg"; //Името на новия файл, но задължително jpg или jpeg. Ако не е jpg трябва да смениш реда в червено. Ако се пропусне този параметър ще се записва във файл name.jpg
$crop = new cropImage($dir.$images, $width, $height, $x, $y, $newfile);
$crop->crop();


echo '<img src="'.$crop->getImage().'" />'; // Връща линк към изображението.
?>
 

Back
Горе