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

时间:2013-05-08 04:12:54

标签: php image-uploading image-upload

我想为每个项目上传多个图片。图像上传成功,但加载这些图片需要花费很多时间。所以,我想将上传的图像保存为jpeg,以便可以轻松加载。

2 个答案:

答案 0 :(得分:1)

将Php库用于图像GD2或使用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);


}

答案 1 :(得分:0)

您可以使用下面的代码来上传压缩图片的图片,也可以将其转换为正常修改的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>

把它放在你的服务器而不是上传图片上,它会在加载时压缩20%,这需要花费不多的时间。