Der Code unten schneidet das Bild gut zu, was ich will, aber für größere Bilder funktioniert es auch nicht. Gibt es eine Möglichkeit, aus dem Bild herauszuzoomen?
Ich könnte mir vorstellen, dass jedes Bild vor dem Zuschneiden ungefähr dieselbe Größe hat, sodass ich jedes Mal gute Ergebnisse erhalte
Code ist
<?php
$image = $_GET['src']; // the image to crop
$dest_image = 'images/cropped_whatever.jpg'; // make sure the directory is writeable
$img = imagecreatetruecolor('200','150');
$org_img = imagecreatefromjpeg($image);
$ims = getimagesize($image);
imagecopy($img,$org_img, 0, 0, 20, 20, 200, 150);
imagejpeg($img,$dest_image,90);
imagedestroy($img);
echo '<img src="'.$dest_image.'" ><p>';
Wenn Sie versuchen, Miniaturansichten zu erstellen, müssen Sie zuerst die Größe des Bildes mit imagecopyresampled();
ändern. Sie müssen die Größe des Bildes so ändern, dass die Größe der kleineren Seite des Bildes der entsprechenden Seite des Daumens entspricht.
Wenn Ihr Quellbild beispielsweise 1280 x 800 Pixel und Ihr Daumen 200 x 150 Pixel groß ist, müssen Sie die Größe Ihres Bildes auf 240 x 150 Pixel ändern und es dann auf 200 x 150 Pixel zuschneiden. Auf diese Weise wird das Seitenverhältnis des Bildes nicht geändert.
Hier ist eine allgemeine Formel zum Erstellen von Miniaturansichten:
$image = imagecreatefromjpeg($_GET['src']);
$filename = 'images/cropped_whatever.jpg';
$thumb_width = 200;
$thumb_height = 150;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
Habe das nicht getestet, aber es sollte funktionieren.
EDIT
Jetzt getestet und funktioniert.
imagecopyresampled()
nimmt einen rechteckigen Bereich von $src_image
mit der Breite $src_w
und Höhe $src_h
an Position ($src_x, $src_y)
und platziere es in einem rechteckigen Bereich von $dst_image
mit der Breite $dst_w
und Höhe $dst_h
an Position ($dst_x, $dst_y)
.
Wenn die Quell- und Zielkoordinaten sowie die Breite und Höhe unterschiedlich sind, wird das Bildfragment entsprechend gedehnt oder verkleinert. Die Koordinaten beziehen sich auf die linke obere Ecke.
Mit dieser Funktion können Bereiche innerhalb desselben Bildes kopiert werden. Wenn sich die Regionen jedoch überschneiden, sind die Ergebnisse nicht vorhersehbar.
Ob $src_w
und $src_h
sind kleiner als $dst_w
und $dst_h
wird das Daumenbild vergrößert. Andernfalls wird es verkleinert.
<?php
$dst_x = 0; // X-coordinate of destination point
$dst_y = 0; // Y-coordinate of destination point
$src_x = 100; // Crop Start X position in original image
$src_y = 100; // Crop Srart Y position in original image
$dst_w = 160; // Thumb width
$dst_h = 120; // Thumb height
$src_w = 260; // Crop end X position in original image
$src_h = 220; // Crop end Y position in original image
// Creating an image with true colors having thumb dimensions (to merge with the original image)
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
// Get original image
$src_image = imagecreatefromjpeg('images/source.jpg');
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, 'images/crop.jpg');
?>
Verbesserte Funktion zum Zuschneiden von Bildern in PHP on the fly.
http://www.example.com/cropimage.php?filename=a.jpg&newxsize=100&newysize=200&constrain=1
Code in cropimage.php
$basefilename = @basename(urldecode($_REQUEST['filename']));
$path = 'images/';
$outPath = 'crop_images/';
$saveOutput = false; // true/false ("true" if you want to save images in out put folder)
$defaultImage = 'no_img.png'; // change it with your default image
$basefilename = $basefilename;
$w = $_REQUEST['newxsize'];
$h = $_REQUEST['newysize'];
if ($basefilename == "") {
$img = $path . $defaultImage;
$percent = 100;
} else {
$img = $path . $basefilename;
$len = strlen($img);
$ext = substr($img, $len - 3, $len);
$img2 = substr($img, 0, $len - 3) . strtoupper($ext);
if (!file_exists($img)) $img = $img2;
if (file_exists($img)) {
$percent = @$_GET['percent'];
$constrain = @$_GET['constrain'];
$w = $w;
$h = $h;
} else if (file_exists($path . $basefilename)) {
$img = $path . $basefilename;
$percent = $_GET['percent'];
$constrain = $_GET['constrain'];
$w = $w;
$h = $h;
} else {
$img = $path . 'no_img.png'; // change with your default image
$percent = @$_GET['percent'];
$constrain = @$_GET['constrain'];
$w = $w;
$h = $h;
}
}
// get image size of img
$x = @getimagesize($img);
// image width
$sw = $x[0];
// image height
$sh = $x[1];
if ($percent > 0) {
// calculate resized height and width if percent is defined
$percent = $percent * 0.01;
$w = $sw * $percent;
$h = $sh * $percent;
} else {
if (isset ($w) AND !isset ($h)) {
// autocompute height if only width is set
$h = (100 / ($sw / $w)) * .01;
$h = @round($sh * $h);
} elseif (isset ($h) AND !isset ($w)) {
// autocompute width if only height is set
$w = (100 / ($sh / $h)) * .01;
$w = @round($sw * $w);
} elseif (isset ($h) AND isset ($w) AND isset ($constrain)) {
// get the smaller resulting image dimension if both height
// and width are set and $constrain is also set
$hx = (100 / ($sw / $w)) * .01;
$hx = @round($sh * $hx);
$wx = (100 / ($sh / $h)) * .01;
$wx = @round($sw * $wx);
if ($hx < $h) {
$h = (100 / ($sw / $w)) * .01;
$h = @round($sh * $h);
} else {
$w = (100 / ($sh / $h)) * .01;
$w = @round($sw * $w);
}
}
}
$im = @ImageCreateFromJPEG($img) or // Read JPEG Image
$im = @ImageCreateFromPNG($img) or // or PNG Image
$im = @ImageCreateFromGIF($img) or // or GIF Image
$im = false; // If image is not JPEG, PNG, or GIF
if (!$im) {
// We get errors from PHP's ImageCreate functions...
// So let's echo back the contents of the actual image.
readfile($img);
} else {
// Create the resized image destination
$thumb = @ImageCreateTrueColor($w, $h);
// Copy from image source, resize it, and paste to image destination
@ImageCopyResampled($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
//Other format imagepng()
if ($saveOutput) { //Save image
$save = $outPath . $basefilename;
@ImageJPEG($thumb, $save);
} else { // Output resized image
header("Content-type: image/jpeg");
@ImageJPEG($thumb);
}
}
Gibt es eine Möglichkeit, das Bild zu verkleinern?
Für einen kleineren serverseitigen/PHP Ansatz ist hier ein Nice jQuery-Plugin .
Man könnte alle notwendigen Anpassungen vornehmen - das Zoomen und das Seitenverhältnis, Art - auf der Client-Seite und die endgültigen Positionen und Größen des zugeschnittenen Bereichs an senden das serverseitig zur endgültigen Manipulation und Speicherung. Die docs sagen so ziemlich, dass du rockst.
HTML Quelltext:-
enter code here
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="image" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
upload.php
enter code here
<?php
$image = $_FILES;
$NewImageName = Rand(4,10000)."-". $image['image']['name'];
$destination = realpath('../images/testing').'/';
move_uploaded_file($image['image']['tmp_name'], $destination.$NewImageName);
$image = imagecreatefromjpeg($destination.$NewImageName);
$filename = $destination.$NewImageName;
$thumb_width = 200;
$thumb_height = 150;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
echo "cropped"; die;
?>
$image = imagecreatefromjpeg($_GET['src']);
Muss ersetzt werden durch:
$image = imagecreatefromjpeg('images/thumbnails/myimage.jpg');
Weil imagecreatefromjpeg()
eine Zeichenkette erwartet.
Das hat bei mir funktioniert.
ref:
http://php.net/manual/en/function.imagecreatefromjpeg.php
pHP 5.5 hat eine Imagecrop-Funktion http://php.net/manual/en/function.imagecrop.php