Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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中将每个图像保存为jpeg格式_Php_Image Uploading_Image Upload - Fatal编程技术网

在php中将每个图像保存为jpeg格式

在php中将每个图像保存为jpeg格式,php,image-uploading,image-upload,Php,Image Uploading,Image Upload,我想为每个项目上载多个图像。图像上载成功,但加载这些图像需要很多时间。因此,我想将上载的图像保存为jpeg格式,以便轻松加载。使用Php库获取图像或使用SimpleImage Php类 $src='uploads/'.$_REQUEST['name']; $newsrc='profiles/'.$_REQUEST['name']; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $targ_w=$_POST['w']; $targ_h=

我想为每个项目上载多个图像。图像上载成功,但加载这些图像需要很多时间。因此,我想将上载的图像保存为jpeg格式,以便轻松加载。

使用Php库获取图像或使用SimpleImage Php类

$src='uploads/'.$_REQUEST['name'];
$newsrc='profiles/'.$_REQUEST['name'];  
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $targ_w=$_POST['w'];
    $targ_h=$_POST['h'];


    $jpeg_quality = 90;
    $img_r = imagecreatefromjpeg($src);//you can chose other option like imagecreatetruecolor($_POST['w'],$_POST['h']) etc
    $dst_r = ImageCreateTrueColor( $_POST['w'],$_POST['h']);

    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);

    imagejpeg($dst_r,$newsrc,$jpeg_quality) or die("Unable to save image");

    imagedestroy($img_r);


}

对图像使用Php库或使用SimpleImage Php类

$src='uploads/'.$_REQUEST['name'];
$newsrc='profiles/'.$_REQUEST['name'];  
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $targ_w=$_POST['w'];
    $targ_h=$_POST['h'];


    $jpeg_quality = 90;
    $img_r = imagecreatefromjpeg($src);//you can chose other option like imagecreatetruecolor($_POST['w'],$_POST['h']) etc
    $dst_r = ImageCreateTrueColor( $_POST['w'],$_POST['h']);

    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);

    imagejpeg($dst_r,$newsrc,$jpeg_quality) or die("Unable to save image");

    imagedestroy($img_r);


}

您可以使用下面的代码上传图像,这将压缩您的图像,您也可以将其转换为jpg与正常修改

<?php
/*
Template Name: image compress
*/
if(isset($_POST['submit'])){

//print each file name
//echo count($_FILES['new_image']['name']);
for($i=0; $i<count($_FILES['new_image']['name']); $i++) 
{       
          $imagename = $_FILES['new_image']['name'][$i];
          $source = $_FILES['new_image']['tmp_name'][$i];
          $a=$_SERVER['DOCUMENT_ROOT'];
          $target = "full/path/where/you/save /image/".$imagename;
          move_uploaded_file($source, $target);

          $imagepath = $imagename;
          //$imagename = explode('.',$imagepath);
          $save = "full/path/where/you/save /image/". $imagepath; //This is the new file you saving
          $file = "full/path/where/you/save /image/". $imagepath; //This is the original file

          list($width, $height) = getimagesize($file) ; 


          $tn = imagecreatetruecolor($width, $height) ; 
          $image = imagecreatefromjpeg($file) ; 
          imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width, $height) ; 
            //.image_type_to_extension(IMAGETYPE_JPEG) image file convert
          imagejpeg($tn, $save,50) ; 

          $thumb = imagecreatetruecolor($newwidth, $newheight);

          /*$save = $a."/demo/mainn/". $imagepath; //This is the new file you saving
          $file = "full/path/where/you/save /image/". $imagepath; //This is the original file

          list($width, $height) = getimagesize($file) ; 

          $modwidth = 176; 

          $diff = $width / $modwidth;

          $modheight = 255; 
          $tn = imagecreatetruecolor($modwidth, $modheight) ; 
          $image = imagecreatefromjpeg($file) ; 
          imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

          imagejpeg($tn, $save, 80); */
        }
    }
    ?>
    <form action="<?php   ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
    <!--<input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />-->
    <input name="new_image[]" type="file" multiple="multiple" />
    <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
    </form>


您可以使用下面的代码上传图像,这将压缩您的图像,您也可以将其转换为jpg与正常修改

<?php
/*
Template Name: image compress
*/
if(isset($_POST['submit'])){

//print each file name
//echo count($_FILES['new_image']['name']);
for($i=0; $i<count($_FILES['new_image']['name']); $i++) 
{       
          $imagename = $_FILES['new_image']['name'][$i];
          $source = $_FILES['new_image']['tmp_name'][$i];
          $a=$_SERVER['DOCUMENT_ROOT'];
          $target = "full/path/where/you/save /image/".$imagename;
          move_uploaded_file($source, $target);

          $imagepath = $imagename;
          //$imagename = explode('.',$imagepath);
          $save = "full/path/where/you/save /image/". $imagepath; //This is the new file you saving
          $file = "full/path/where/you/save /image/". $imagepath; //This is the original file

          list($width, $height) = getimagesize($file) ; 


          $tn = imagecreatetruecolor($width, $height) ; 
          $image = imagecreatefromjpeg($file) ; 
          imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width, $height) ; 
            //.image_type_to_extension(IMAGETYPE_JPEG) image file convert
          imagejpeg($tn, $save,50) ; 

          $thumb = imagecreatetruecolor($newwidth, $newheight);

          /*$save = $a."/demo/mainn/". $imagepath; //This is the new file you saving
          $file = "full/path/where/you/save /image/". $imagepath; //This is the original file

          list($width, $height) = getimagesize($file) ; 

          $modwidth = 176; 

          $diff = $width / $modwidth;

          $modheight = 255; 
          $tn = imagecreatetruecolor($modwidth, $modheight) ; 
          $image = imagecreatefromjpeg($file) ; 
          imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

          imagejpeg($tn, $save, 80); */
        }
    }
    ?>
    <form action="<?php   ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
    <!--<input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />-->
    <input name="new_image[]" type="file" multiple="multiple" />
    <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
    </form>

核对