1

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

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

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

来源=> /我的文件夹/

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

目的地=>我的文件夹/新建/

或者可能是我的方法是错误的,如果是这样,请告诉什么是正确的方法。

非常感谢任何帮助。

4

4 回答 4

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();
}
?>
于 2014-10-10T11:28:20.790 回答
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');
于 2014-10-09T11:36:11.817 回答
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);

尝试这个

于 2014-10-09T11:45:52.687 回答
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");
    }
}
于 2014-10-09T12:01:35.430 回答