本文介绍了将裁剪后的图片保存为不同的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用裁剪图片。



这是我上传图片和裁剪的表单。

  form id =upload_formenctype =multipart / form-datamethod =postaction =upload.phponsubmit =return checkForm()> 
<! - hidden crop params - >
< input type =hiddenid =x1name =x1/>
< input type =hiddenid =y1name =y1/>
< input type =hiddenid =x2name =x2/>
< input type =hiddenid =y2name =y2/>

< div>< input type =filename =image_fileid =image_fileonchange =fileSelectHandler()/>< / div>

< div class =error>< / div>

< div class =step2>
< h2>第2步:请选择裁剪区域< / h2>
< img id =preview/>

< div class =info>
< label>文件大小< / label> < input type =textid =filesizename =filesize/>
< label>类型< / label> < input type =textid =filetypename =filetype/>
< label>图片尺寸< / label> < input type =textid =filedimname =filedim/>
< label> W< / label> < input type =textid =wname =w/>
< label> H< / label> < input type =textid =hname =h/>
< / div>

< input type =submitvalue =上传/>
< / div>
< / form>

upload.php档案将裁剪后的图片上传到

 <?php function uploadImageFile(){//注意:GD库是此函数必需的

if($ _SERVER ['REQUEST_METHOD'] =='POST'){
$ iWidth = $ iHeight = 200; //所需的图像结果维度
$ iJpgQuality = 90;

if($ _FILES){

//如果没有错误和大小小于250kb
if(!$ _FILES ['image_file'] ['error' ]&&& $ _FILES ['image_file'] ['size']< 250 * 1024){
if(is_uploaded_file($ _ FILES ['image_file'] ['tmp_name'])){

//新的唯一文件名
$ sTempFileName ='avatar /'。 md5(time()。rand());

//将上传的文件移动到缓存文件夹
move_uploaded_file($ _ FILES ['image_file'] ['tmp_name'],$ sTempFileName);

//将文件权限更改为644
@chmod($ sTempFileName,0644);

if(file_exists($ sTempFileName)&&&&>($ sTempFileName)> 0){
$ aSize = getimagesize($ sTempFileName); //尝试获取图像信息
if(!$ aSize){
@unlink($ sTempFileName);
return;
}

//检查图像类型
switch($ aSize [2]){
case IMAGETYPE_JPEG:
$ sExt ='.jpg' ;

//从文件创建一个新图像
$ vImg = @imagecreatefromjpeg($ sTempFileName);
break;
/ * case IMAGETYPE_GIF:
$ sExt ='.gif';

//从文件创建一个新图像
$ vImg = @imagecreatefromgif($ sTempFileName);
break; * /
case IMAGETYPE_PNG:
$ sExt ='.png';

//从文件创建一个新图像
$ vImg = @imagecreatefrompng($ sTempFileName);
break;
默认:
@unlink($ sTempFileName);
return;
}

//创建一个新的真彩色图像
$ vDstImg = @imagecreatetruecolor($ iWidth,$ iHeight);

//通过重新采样复制和调整图像的一部分
imagecopyresampled($ vDstImg,$ vImg,0,0,(int)$ _ POST ['x1'], _POST ['y1'],$ iWidth,$ iHeight,(int)$ _ POST ['w'],(int)$ _ POST ['h']);

//定义结果映像文件名
$ sResultFileName = $ sTempFileName。 $ sExt;

//将图像输出到文件
imagejpeg($ vDstImg,$ sResultFileName,$ iJpgQuality);
@unlink($ sTempFileName);

return $ sResultFileName;
}
}
}
}
}
}

$ sImage = uploadImageFile();
echo'< img src ='。$ sImage。'/>';
?>

我的问题:



现在只要在 头像目录中上传裁剪后的图片,宽度和高度为200像素。



的宽度和高度为700像素 / p>

任何帮助将不胜感激。

解决方案

向函数中添加参数,例如 uploadImageFile($ dirName,$ iWidth,$ iHeight)然后以不同的大小调用它多次

 < $ php 
function uploadImageFile(){//注意:此函数需要GD库

if($ _SERVER ['REQUEST_METHOD'] == 'POST'){
$ iJpgQuality = 90;

if($ _FILES){
//如果没有错误和大小小于250kb
if(!$ _FILES ['image_file'] ['error']&& ; $ _FILES ['image_file'] ['size'] if(is_uploaded_file($ _ FILES ['image_file'] ['tmp_name'])){
if is_dir('avatar')){
mkdir('avatar');
}
//新的唯一文件名
$ sTempFileName ='avatar /'。 md5(time()。rand());
//将上传的文件移动到缓存文件夹
move_uploaded_file($ _ FILES ['image_file'] ['tmp_name'],$ sTempFileName);

//将文件权限更改为644
@chmod($ sTempFileName,0644);

$ sResultFileName = copyImageFile('avatar',$ sTempFileName,200,200,$ iJpgQuality);
if($ sResultFileName){
copyImageFile('avatar1',$ sTempFileName,500,500);
copyImageFile('avatar2',$ sTempFileName,700,700);
@unlink($ sTempFileName);

return $ sResultFileName;
}
}
}
}
}
return false;
}

function copyImageFile($ dirName,$ originImageName,$ iWidth,$ iHeight,$ iJpgQuality = 90){
if(file_exists($ originImageName)&& ($ originImageName)> 0){
$ aSize = getimagesize($ originImageName); //尝试获取图像信息
if(!$ aSize){
@unlink($ originImageName);
return;
}

//检查图像类型
switch($ aSize [2]){
case IMAGETYPE_JPEG:
$ sExt ='.jpg' ;
$ vImg = @imagecreatefromjpeg($ originImageName);
break;
/ * case IMAGETYPE_GIF:
$ sExt ='.gif';

//从文件创建一个新图像
$ vImg = @imagecreatefromgif($ sTempFileName);
break; * /
case IMAGETYPE_PNG:
$ sExt ='.png';
$ vImg = @imagecreatefrompng($ originImageName);
break;
默认值:
@unlink($ originImageName);
return;
}

//创建一个新的真彩色图像
$ vDstImg = @imagecreatetruecolor($ iWidth,$ iHeight);

//通过重新采样复制和调整图像的一部分
imagecopyresampled($ vDstImg,$ vImg,0,0,(int)$ _ POST ['x1'], _POST ['y1'],$ iWidth,$ iHeight,(int)$ _ POST ['w'],(int)$ _ POST ['h']);

//定义结果图像文件名
if(!is_dir($ dirName)){
mkdir($ dirName);
}
$ newImageName = $ dirName。 DIRECTORY_SEPARATOR。 md5(time()。rand())。 $ sExt;

//将图像输出到文件
imagejpeg($ vDstImg,$ newImageName,$ iJpgQuality);
// @ unlink($ sTempFileName);

return $ newImageName;
}

return false;
}

$ sImage = uploadImageFile();
echo'< img src ='。$ sImage。'/>';
?>


I am using jcrop to crop images.

This is the form that i upload the image and crop.

                <form id="upload_form" enctype="multipart/form-data" method="post" action="upload.php" onsubmit="return checkForm()">
                <!-- hidden crop params -->
                <input type="hidden" id="x1" name="x1" />
                <input type="hidden" id="y1" name="y1" />
                <input type="hidden" id="x2" name="x2" />
                <input type="hidden" id="y2" name="y2" />

                <div><input type="file" name="image_file" id="image_file" onchange="fileSelectHandler()" /></div>

                <div class="error"></div>

                <div class="step2">
                    <h2>Step2: Please select a crop region</h2>
                    <img id="preview" />

                    <div class="info">
                        <label>File size</label> <input type="text" id="filesize" name="filesize" />
                        <label>Type</label> <input type="text" id="filetype" name="filetype" />
                        <label>Image dimension</label> <input type="text" id="filedim" name="filedim" />
                        <label>W</label> <input type="text" id="w" name="w" />
                        <label>H</label> <input type="text" id="h" name="h" />
                    </div>

                    <input type="submit" value="Upload" />
                </div>
            </form>

upload.php file which upload cropped image to avatar directory.

<?php function uploadImageFile() { // Note: GD library is required for this function

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $iWidth = $iHeight = 200; // desired image result dimensions
    $iJpgQuality = 90;

    if ($_FILES) {

        // if no errors and size less than 250kb
        if (! $_FILES['image_file']['error'] && $_FILES['image_file']['size'] < 250 * 1024) {
            if (is_uploaded_file($_FILES['image_file']['tmp_name'])) {

                // new unique filename
                $sTempFileName = 'avatar/' . md5(time().rand());

                // move uploaded file into cache folder
                move_uploaded_file($_FILES['image_file']['tmp_name'], $sTempFileName);

                // change file permission to 644
                @chmod($sTempFileName, 0644);

                if (file_exists($sTempFileName) && filesize($sTempFileName) > 0) {
                    $aSize = getimagesize($sTempFileName); // try to obtain image info
                    if (!$aSize) {
                        @unlink($sTempFileName);
                        return;
                    }

                    // check for image type
                    switch($aSize[2]) {
                        case IMAGETYPE_JPEG:
                            $sExt = '.jpg';

                            // create a new image from file 
                            $vImg = @imagecreatefromjpeg($sTempFileName);
                            break;
                        /*case IMAGETYPE_GIF:
                            $sExt = '.gif';

                            // create a new image from file 
                            $vImg = @imagecreatefromgif($sTempFileName);
                            break;*/
                        case IMAGETYPE_PNG:
                            $sExt = '.png';

                            // create a new image from file 
                            $vImg = @imagecreatefrompng($sTempFileName);
                            break;
                        default:
                            @unlink($sTempFileName);
                            return;
                    }

                    // create a new true color image
                    $vDstImg = @imagecreatetruecolor( $iWidth, $iHeight );

                    // copy and resize part of an image with resampling
                    imagecopyresampled($vDstImg, $vImg, 0, 0, (int)$_POST['x1'], (int)$_POST['y1'], $iWidth, $iHeight, (int)$_POST['w'], (int)$_POST['h']);

                    // define a result image filename
                    $sResultFileName = $sTempFileName . $sExt;

                    // output image to file
                    imagejpeg($vDstImg, $sResultFileName, $iJpgQuality);
                    @unlink($sTempFileName);

                    return $sResultFileName;
                }
            }
        }
    }
}
}

  $sImage = uploadImageFile();
  echo '<img src="'.$sImage.'" />';
  ?>

My Question:

Right now it just upload the cropped image in avatar directory with width and height of 200px.

I want to also upload that cropped image in to two other directories

  1. avatar1 with width and height of 500px

  2. avatar2 with width and height of 700px

Any help will be appreciated.

解决方案

Add arguments to your function, e.g. uploadImageFile($dirName, $iWidth, $iHeight) and then invoke it multiple times with different sizes

<?php 
    function uploadImageFile() { // Note: GD library is required for this function

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $iJpgQuality = 90;

        if ($_FILES) {
            // if no errors and size less than 250kb
            if (! $_FILES['image_file']['error'] && $_FILES['image_file']['size'] < 250 * 1024) {
                if (is_uploaded_file($_FILES['image_file']['tmp_name'])) {
                    if (!is_dir('avatar')) {
                        mkdir('avatar');
                    }
                    // new unique filename
                    $sTempFileName = 'avatar/' . md5(time().rand());
                    // move uploaded file into cache folder
                    move_uploaded_file($_FILES['image_file']['tmp_name'], $sTempFileName);

                    // change file permission to 644
                    @chmod($sTempFileName, 0644);

                    $sResultFileName = copyImageFile('avatar', $sTempFileName, 200, 200, $iJpgQuality);
                    if ($sResultFileName) {
                        copyImageFile('avatar1', $sTempFileName, 500, 500);
                        copyImageFile('avatar2', $sTempFileName, 700, 700);
                        @unlink($sTempFileName);

                        return $sResultFileName;
                    }
                }
            }
        }
    }
    return false;
}

function copyImageFile($dirName, $originImageName, $iWidth, $iHeight, $iJpgQuality = 90) {
    if (file_exists($originImageName) && filesize($originImageName) > 0) {        
        $aSize = getimagesize($originImageName); // try to obtain image info
        if (!$aSize) {
            @unlink($originImageName);
            return;
        }

        // check for image type
        switch($aSize[2]) {
            case IMAGETYPE_JPEG:
                $sExt = '.jpg';
                $vImg = @imagecreatefromjpeg($originImageName);
                break;
            /*case IMAGETYPE_GIF:
                $sExt = '.gif';

                // create a new image from file 
                $vImg = @imagecreatefromgif($sTempFileName);
                break;*/
            case IMAGETYPE_PNG:
                $sExt = '.png';
                $vImg = @imagecreatefrompng($originImageName);
                break;
            default:
                @unlink($originImageName);
                return;
        }

        // create a new true color image
        $vDstImg = @imagecreatetruecolor( $iWidth, $iHeight );

        // copy and resize part of an image with resampling
        imagecopyresampled($vDstImg, $vImg, 0, 0, (int)$_POST['x1'], (int)$_POST['y1'], $iWidth, $iHeight, (int)$_POST['w'], (int)$_POST['h']);

        // define a result image filename        
        if (!is_dir($dirName)) {
            mkdir($dirName);
        }
        $newImageName = $dirName . DIRECTORY_SEPARATOR . md5(time().rand()) . $sExt;

        // output image to file
        imagejpeg($vDstImg, $newImageName, $iJpgQuality);
        //@unlink($sTempFileName);

        return $newImageName;
    }

    return false;
}

$sImage = uploadImageFile();
echo '<img src="'.$sImage.'" />';
?>

这篇关于将裁剪后的图片保存为不同的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:26