Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中调整图片大小?_Php_Image_Resize - Fatal编程技术网

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

如何在PHP中调整图片大小?,php,image,resize,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

下面是我在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);

我认为你必须这样做:

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脚本的其余部分进行操作

或者您可以这样做:查看:

**使用PHP调整图像大小**

你有一个例子吗?我不知道你是什么意思。或者你可以做:看看:谢谢你的帮助!!:-一切都与你的链接!没问题:)投票支持我的答案,以帮助他人
   **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...';
    }
}