i bir görüntüyü yeniden boyutlandırmak için daha gd veya imagemagic olduğunu bilmek gerekir
I ImageMagick daha çok seviyorum. Ama ben biliyorum GD çok oldukça iyi.
İşte PHP kullanarak bir görüntüyü yeniden boyutlandırmak için nasıl bir örnek:
<?php
if(!extension_loaded('imagick')) {
dl('imagick.so');
}
$img = strip_tags($_GET['imagename']);
if(isset($_GET['size'])) {
$size = strip_tags($_GET['size']);
} else {
$size = 0;
}
if(isset($_GET['vsize'])) {
$vsize = strip_tags($_GET['vsize']);
} else {
$vsize = 0;
}
$image = new Imagick($img);
$image->thumbnailImage($size, $vsize);
header("Content-type: image/png");
print $image;
?>
Here Ben örnek var bağlantıdır. Sadece doğru söz buna sahip kopyalandı. Tüm krediler yazdı kişiye gidin.
Below is a thumbnailer I wrote in PHP. I've stripped out the bits that add drop shadows and borders (Don't think i've broken it but haven't tested). This uses the GD library in PHP and I've always been happy with the results.
Not: Muhtemelen daha fazla şerit olabilir - bu sayfa arka planını, vb eşleşir böylece örneğin bu minik BG rengini ayarlar ..
Bu durumda, bu gibi olacağını söyledi:
thumbnail.php?size=400&image=SomeImage.jpg
Sadece hafif sorunu büyük dosyaları (modern dijital kameralar, yani çok yüksek kalite) ile bellek sorunları olabilir. Ben nadiren de olsa bu sorunu vurmak - boyutu web sunucusu gibi bir kullanıcı tarafından yüklenen edilemez genellikle hiçbir şey buna izin vermez.
<?php
$defaultsize = 400;
$defaultimage = "images/error.jpg";
ini_set("memory_limit", "32M");
$red = isset($_REQUEST['r']) ? $_REQUEST['r'] : 255;
$green = isset($_REQUEST['g']) ? $_REQUEST['g'] : 255;
$blue = isset($_REQUEST['b']) ? $_REQUEST['b'] : 255;
if(!isset($_REQUEST['size'])) {
$maxWidth=$defaultsize;
$maxHeight=$defaultsize;
} else {
$maxWidth=$_REQUEST['size'];
$maxHeight=$_REQUEST['size'];
}
if(!isset($_REQUEST['image'])) {
$picurl = $defaultimage;
} else {
$picurl = "../" . stripslashes($_REQUEST['image']);
}
//Find out about source file
$srcDetails = @getimagesize($picurl);
if($srcDetails) {
$srcWidth=$srcDetails[0];
$srcHeight=$srcDetails[1];
} else {
$srcWidth=$maxWidth;
$srcHeight=$maxHeight;
}
if($srcWidth/$srcHeight < $maxWidth/$maxHeight) {
//Too wide
$width = $maxHeight / $srcHeight * $srcWidth;
$height = $maxHeight / $srcHeight * $srcHeight;
} else {
//Too tall
$width = $maxWidth / $srcWidth * $srcWidth;
$height = $maxWidth / $srcWidth * $srcHeight;
}
switch ($srcDetails[2]) {
case 1: //GIF
$srcImage = ImagecreateFromGIF($picurl);
break;
case 2: //JPEG
$srcImage = ImagecreateFromJPEG($picurl);
break;
case 3: //PNG
$srcImage = ImagecreateFromPNG($picurl);
break;
case 6: //WBMP
$srcImage = ImagecreateFromWBMP($picurl);
break;
default:
//Possibly add some "Unknown File Type" error code here. However, if we do't return an image, we will error nicely later anyway
break;
}
if(@!$srcImage) {
// The nice error for no source image (include error mail to yourself here if you want...)
$srcImage = imagecreate($maxWidth, $maxHeight); /* Create a blank image */
$bgc = imagecolorallocate($srcImage, 255, 255, 255);
$tc = imagecolorallocate($srcImage, 0, 0, 0);
imagefilledrectangle($srcImage, 0, 0, 150, 30, $bgc);
/* Output an errmsg */
imagestring($srcImage, 4, 5, 5, "Error resizing image", $tc);
imagestring($srcImage, 4, 5, 20, "Tech support department", $tc);
imagestring($srcImage, 4, 5, 35, "has been informed", $tc);
}
//Create thumbnail
$thumb = imagecreatetruecolor ($width, $height);
$bg = ImageColorAllocate($thumb, $red, $green, $blue);
imagefill ($thumb, 0, 0, $bg);
//Add the image itself
Imagecopyresized ($thumb, $srcImage, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
//Add a black border
imageline($thumb, 0, 0, 0, $height, $black);
imageline($thumb, 0, 0, $width, 0, $black);
imageline($thumb, 0, $height, $width, $height, $black);
imageline($thumb, $width, $height, $width, 0, $black);
//output header
//I leave this so late so if there ARE any errors, they are displayed as text not a broken image
//(this will happen when looking at the thumnailer directly but will display as a broken image in a webpage still)
header("Content-type: image/PNG");
imagePNG($thumb);
//Clear up memory
imagedestroy($srcImage);
>