简体   繁体   中英

Image is not saving usnig imagejpeg function

I am trying to resize the image and trying to save that image in my folder. But my image is not saving in folder. Below is the code:

$add_image=$_FILES["add_image"]["name"];
if($add_image)

{   

    $extension = getExtension($add_image);
    $extension = strtolower($extension);

    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
        {

            echo "Unknown Image extension";

        }

        else
        {
            $size=filesize($_FILES['add_image']['tmp_name']);
            if($extension=="jpg" || $extension=="jpeg" )
            {
                $uploadedfile = $_FILES['file']['tmp_name'];
                $src = imagecreatefromjpeg($uploadedfile);
            }
            else if($extension=="png")
            {
                $uploadedfile = $_FILES['file']['tmp_name'];
                $src = imagecreatefrompng($uploadedfile);
            }
           else 
            {
               $src = imagecreatefromgif($uploadedfile);
            }
            list($width,$height)=getimagesize($uploadedfile);


            $newwidth=500;
            $newheight=($height/$width)*$newwidth;
            $tmp=imagecreatetruecolor($newwidth,$newheight);


            $newwidth1=300;
            $newheight1=($height/$width)*$newwidth1;
            $tmp1=imagecreatetruecolor($newwidth1,$newheight1);

            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

            imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);




            //$filename = "images/". $_FILES['file']['name'];
        }
    $rand = rand(0,999);

    $rename_image = $rand.'_'.$add_image;   

    $uploaddir = 'admin/pages/images/'.$upload_sale.'/';

    $uploadfile = $uploaddir . $add_image;
    imagejpeg($tmp,$uploadfile,100);
    imagejpeg($tmp1,$uploadfile,100);
    imagedestroy($src);
    imagedestroy($tmp);
    imagedestroy($tmp1);

Use Some class to Resize image like this one http://phpimagemagician.jarrodoberto.com/ I found this link is very useful.

 require_once('php_image_magician.php');
 if(isset($_POST['submit']))
 {
  $path = 'upload/';
  $actual_image_name=$_FILES['add_image']['name'];
  $tmp = $_FILES['add_image']['tmp_name'];
  if(move_uploaded_file($tmp, $path.$actual_image_name))
   {
    $magicianObj = new imageLib($path.$actual_image_name);
    $magicianObj -> resizeImage(198, 259);//size you want to resize
    $magicianObj -> saveImage($path.$actual_image_name, 100);
    echo "success";
   } else{
    echo "failure";
   }
 }

Html part like this

<form action="" method="post" enctype="multipart/form-data">

<input type="file" name="add_image" >
<input type="submit" name="submit">

</form>

I just showing the logic, use it wisely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM