从现有图像创建较小尺寸的缩略图

时间:2014-10-09 11:31:05

标签: php image file-upload thumbnails imagecreatefrompng

我正在将图片上传到我的文件夹,并且已经从上传的图片中创建了缩略图。

/*
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);

但是,它根本没有创造任何东西,我已经上传了图像

  

来源=> / myfolder /

现在,如何创建较小尺寸的缩略图并将其保存到

  

目的地=> myfolder / New /

或者可能是我的方法是错误的,如果是这样,请说出正确的方法。

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:2)

试试这个它适用于.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();
}
?>

答案 1 :(得分:0)

我总是发现图像处理很痛苦所以我使用了一个名为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');

答案 2 :(得分:0)

你可以这样做

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);

试试这个

答案 3 :(得分:0)

这个功能可以帮助你:

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");
    }
}