如何在PHP中调整图片大小?

时间:2014-10-15 10:03:09

标签: php image resize

以下是我在PHP中的文件夹中上传图片的代码。

if(isset($_GET['id']))
{
    $id= $_GET['id'];

    $folder = 'uploads/';
    $filename = $id.'.jpg';


    $uploaddir = './uploads/';

     // create new directory with 744 permissions if it does not exist yet
     // owner will be the user/group the PHP script is run under
     if ( !file_exists($folder) ) {
        mkdir ($folder, 0744);
     }

    $data = $_POST['base64data'];

    $type = '';

    list($type, $data) = explode(';', $data);
    list(, $data)      = explode(',', $data);
    $data = base64_decode($data);

    $file_path = $folder.$filename;

    echo $_POST['base64data'];

    file_put_contents($file_path, $data);
}

但是,如何将上传的图片调整为我想要的尺寸?

我发现了一个PHP函数,但我不确定如何在我的代码中实现它...这是imagecopyresampled

// Le fichier
$filename = 'test.jpg';

// Définition de la largeur et de la hauteur maximale
$width = 200;
$height = 200;

// Content type
header('Content-Type: image/jpeg');

// Cacul des nouvelles dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Redimensionnement
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Affichage
imagejpeg($image_p, null, 100);

2 个答案:

答案 0 :(得分:0)

我认为你必须这样做:

if(isset($_GET['id']))
    {
    /*put the code which resize your image here*/
        $id= $_GET['id'];

        $folder = 'uploads/';
        $filename = $id.'.jpg';


        $uploaddir = './uploads/';

         // create new directory with 744 permissions if it does not exist yet
         // owner will be the user/group the PHP script is run under
         if ( !file_exists($folder) ) {
            mkdir ($folder, 0744);
         }

        $data = $_POST['base64data'];

        $type = '';

        list($type, $data) = explode(';', $data);
        list(, $data)      = explode(',', $data);
        $data = base64_decode($data);

        $file_path = $folder.$filename;

        echo $_POST['base64data'];

        file_put_contents($file_path, $data);
    }

在使用imagecopyresampled调整图像大小后的评论中(使用本地图像尝试),您必须获取内容类型,然后按照imagecopyresampled脚本的其余部分进行操作。

或者你可以做到:检查出来:

nimrodstech.com/php-image-resize

github.com/Nimrod007/PHP_image_resize

答案 1 :(得分:0)

   **Resize images with PHP**


 <?php

     if ($_FILES['fileToUpload']['error'] > 0) {
        echo "Error: " . $_FILES['fileToUpload']['error'] . "<br />";
    } else {
        // array of valid extensions
        $validExtensions = array('.jpg', '.jpeg', '.gif', '.png');
        // get extension of the uploaded file
        $fileExtension = strrchr($_FILES['fileToUpload']['name'], ".");
        // check if file Extension is on the list of allowed ones
        if (in_array($fileExtension, $validExtensions)) {
            $newNamePrefix = time() . '_';
            $manipulator = new ImageManipulator($_FILES['fileToUpload']['tmp_name']);
            // resizing to 200x200
            $newImage = $manipulator->resample(200, 200);
            // saving file to uploads folder
            $manipulator->save('uploads/' . $newNamePrefix . $_FILES['fileToUpload']['name']);
            echo 'Done ...';
        } else {
            echo 'You must upload an image...';
        }
    }

**Crop  images with PHP**

if ($_FILES['fileToUpload']['error'] > 0) {
    echo "Error: " . $_FILES['fileToUpload']['error'] . "<br />";
} else {
    // array of valid extensions
    $validExtensions = array('.jpg', '.jpeg', '.gif', '.png');
    // get extension of the uploaded file
    $fileExtension = strrchr($_FILES['fileToUpload']['name'], ".");
    // check if file Extension is on the list of allowed ones
    if (in_array($fileExtension, $validExtensions)) {
        $newNamePrefix = time() . '_';
        $manipulator = new ImageManipulator($_FILES['fileToUpload']['tmp_name']);
        $width  = $manipulator->getWidth();
        $height = $manipulator->getHeight();
        $centreX = round($width / 2);
        $centreY = round($height / 2);
        // our dimensions will be 200x130
        $x1 = $centreX - 100; // 200 / 2
        $y1 = $centreY - 65; // 130 / 2

        $x2 = $centreX + 100; // 200 / 2
        $y2 = $centreY + 65; // 130 / 2

        // center cropping to 200x130
        $newImage = $manipulator->crop($x1, $y1, $x2, $y2);
        // saving file to uploads folder
        $manipulator->save('uploads/' . $newNamePrefix . $_FILES['fileToUpload']['name']);
        echo 'Done ...';
    } else {
        echo 'You must upload an image...';
    }
}