从现有图像创建较小尺寸的缩略图
问题描述:
我正在将图像上传到我的文件夹,并且我已经从上传的图像创建了一个缩略图。从现有图像创建较小尺寸的缩略图
/*
save this image to try --
http://images.dpchallenge.com/images_challenge/0-999/319/800/Copyrighted_Image_Reuse_Prohibited_157378.jpg
*/
$source = 'H:/xampp/htdocs/myfolder/new.jpg';
$destination = $_SERVER['DOCUMENT_ROOT'] . 'myfolder/New/';
$quality = 60;
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($source);
}
echo $image;
print(imagejpeg($image, $destination, $quality));
imagejpeg($image, $destination, $quality);
但是,它也没有创造任何东西,我alreday已经
源=>/MyFolder中/
现在,如何创建一个缩略图上传的图片更小的尺寸并保存到
目的地=> MyFolder文件/新建/
或可能我的做法是错误的,如果是的话请告诉什么是正确的方式。
任何帮助真的很感激。
答
尝试th是它会为JPG格式的作品和.png图像
<?php
$file = 'H:/xampp/htdocs/myfolder/phool.png';
$pathToSave = 'H:/xampp/myfolder/New/';
$what = getimagesize($file);
$file_name = basename($file);
//print "MIME ".$what['mime'];
switch(strtolower($what['mime']))
{
case 'image/png':
$img = imagecreatefrompng($file);
$new = imagecreatetruecolor($what[0],$what[1]);
imagecopy($new,$img,0,0,0,0,$what[0],$what[1]);
header('Content-Type: image/png');
imagepng($new,$pathToSave.$file_name,9);
imagedestroy($new);
break;
case 'image/jpeg':
$img = imagecreatefromjpeg($file);
$new = imagecreatetruecolor($what[0],$what[1]);
imagecopy($new,$img,0,0,0,0,$what[0],$what[1]);
header('Content-Type: image/jpeg');
imagejpeg($new,$pathToSave.$file_name);
imagedestroy($new);
break;
case 'image/gif':
$img = imagecreatefromgif($file);
break;
default: die();
}
?>
答
我总是发现图像处理的痛苦,所以我使用一个名为Intervention的库。
这里是例如根据您的方案,以创建一个缩略图:
// open an image file
$img = Image::make('H:/xampp/htdocs/myfolder/new.jpg');
// now you are able to resize the instance
$img->resize(75, 75);
// save the image as a new file
$img->save($destination.'new.jpg');
答
,你可以做这样的
list($sourceWidth,$sourceHeight) = getimagesize($filepath)
$destWidth = round($sourceWidth/2);
$destHeight = round($sourceHeight/2);
$sourceImage = imagecreatefromjpeg($filepath);
$destinationImage = imagecreatefromtrue($destWidth,$destHeight);
//Now Main function for resizing purpose
imagecopyresized($destinationImage,$sourceImage,0,0,0,0,$destWidth,$destHeight,$sourceWidth,$sourceHeight);
// Now You can save Image
imagejpeg($destImage,$pathToSave);
试试这个
答
可以此功能可以帮助您:
function makeThumbnails($updir, $img, $id)
{
$thumbnail_width = 134;
$thumbnail_height = 189;
$thumb_beforeword = "thumb";
$arr_image_details = getimagesize("$updir" . $id . '_' . "$img"); // pass id to thumb name
$original_width = $arr_image_details[0];
$original_height = $arr_image_details[1];
if ($original_width > $original_height) {
$new_width = $thumbnail_width;
$new_height = intval($original_height * $new_width/$original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width * $new_height/$original_height);
}
$dest_x = intval(($thumbnail_width - $new_width)/2);
$dest_y = intval(($thumbnail_height - $new_height)/2);
if ($arr_image_details[2] == 1) {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
}
if ($arr_image_details[2] == 2) {
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
}
if ($arr_image_details[2] == 3) {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
}
if ($imgt) {
$old_image = $imgcreatefrom("$updir" . $id . '_' . "$img");
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresized($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height);
$imgt($new_image, "$updir" . $id . '_' . "$thumb_beforeword" . "$img");
}
}
你能帮助我在安装它,我使用Drupal的 – Hitesh 2014-10-09 11:43:21
对不起,我从来没有使用Drupal的前 – 2014-10-09 11:44:57
NP!告诉如何安装它在php – Hitesh 2014-10-09 11:59:39