Question

I want to upload multiple images for each item.Images are uploading successfully but it takes a lot time to load those images.So,i want to save the uploaded image as jpeg so that it can be loaded easily.

Was it helpful?

Solution

Use Php library for images GD2 or use SimpleImage PHP class

$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);


}

OTHER TIPS

you can use below code for upload image that will compress your image and you can also convert it in to jpg with normal modification.

<?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>

put this on you server than upload image it will compress 20% when it loads that take not much time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top