تاریخ امروز:7 اردیبهشت 1403
تکه کد تغییر سایز عکس با php

تکه کد تغییر سایز عکس با PHP

تکه کد تغییر سایز عکس با php

تکه کد تغییر سایز عکس با php

با سلام خدمت همه‌ی دوستان

توسط تکه کد زیر می‌تونید سایز عکس رو تغییر بدید. لازم به ذکره این تیکه کد فقط برای تست هستش و توی پروژه‌ی واقعی استفاده نکنیدش همینجوری 🙂

   ob_start();
   function calculate_width_height($width, $height,$width_needed, $height_needed)
   {
      if ($width <= $width_needed and $height <= $height_needed)
      {

         $l_new_width=$width;
         $l_new_height=$height;
      }
      else
      {
         if ($height > $height_needed)
         {
            $l_percentage_height =$height_needed/($height/100);
            $l_new_width=($width/100)*$l_percentage_height;
            $l_new_height=$height_needed;
            if ($l_new_width > $width_needed)
            {

               $l_percentage_width =$width_needed/($width/100);
               $l_new_height=($height/100)*$l_percentage_width;
               $l_new_width=$width_needed;
            }
         }
         else
         {
            $l_percentage_width =$width_needed/($width/100);
            $l_new_height=($height/100)*$l_percentage_width;
            $l_new_width=$width_needed;
            if ($l_new_height > $height_needed)
            {
               $l_percentage_height =$height_needed/($height/100);
               $l_new_width=($width/100)*$l_percentage_height;
               $l_new_height=$height_needed;
            }
         }
      }
   $calculated_width_height[] = round($l_new_width);
   $calculated_width_height[] = round($l_new_height);
   return $calculated_width_height;
   }
   function resize($filename, $dest, $width, $height, $type=''){
       if(@imagecreatefromgif($filename)){
           $img=imagecreatefromgif($filename);
           $type_r='gif';
       }
       elseif(@imagecreatefromjpeg($filename)){
           $img=imagecreatefromjpeg($filename);
           $type_r='jpg';
       }
       elseif(@imagecreatefrompng($filename)){
           $img=imagecreatefrompng($filename);
           $type_r='png';
       }
       elseif(@imagecreatefromwbmp($filename)){
           $img=imagecreatefromwbmp($filename);
           $type_r='bmp';
       }
       else{
           return 'Cannot open file';
       }
       $type=($type=='')?$type_r:'';
       $img_n= @ imagecreatetruecolor ($width, $height);
       @ imagecopyresampled($img_n, $img, 0, 0, 0, 0, $width, $height, imagesx($img), imagesy($img));
       if($type=='gif'){
           imagegif($img_n, $dest);
       }
       elseif($type=='jpg'){
          @ imagejpeg($img_n, $dest);
       }
       elseif($type=='png'){
          @ imagepng($img_n, $dest);
       }
       elseif($type=='bmp'){
          @ imagewbmp($img_n, $dest);
       }
       Return true;
   }
   function upload_file($root_dir,$file_name,$file_name_tmp,$lc_width,$lc_height)
   {
      $lc_max_filesize=2001000;
      $filesize=filesize($file_name_tmp);
      $sub_server=substr($_SERVER["HTTP_REFERER"],7,9);
      $uploaddir = "/get_on_top/image_resize/uploads";
      if (
         !file_exists($HTTP_SERVER_VARS["DOCUMENT_ROOT"].$uploaddir) ||
         2 != (fileperms($HTTP_SERVER_VARS["DOCUMENT_ROOT"].$uploaddir) & 2) ||
         1 != (fileperms($HTTP_SERVER_VARS["DOCUMENT_ROOT"].$uploaddir) & 1)
         )
      {
         $error="tets";
      }
      $fileext = substr($file_name, -4);
      if (strtolower($fileext) != ".jpg" and strtolower($fileext) != ".gif")
      {
         print "<b>For security reasons only the uploading of .jpg and .gif files is allowed.</b><br>\n";
         print "<a href=\"javascript:history.back();\">Click here to go back.</a><br>";
         exit;
      };
      $loc = $uploaddir."/".$file_name;
      $fullloc = $root_dir.$loc;
      $upfile = $file_name_tmp;
      if ($filesize>$lc_max_filesize)
      {
         $lc_message= "File is too big, may not be bigger then 2MB";
      }
      else
      {
         if(move_uploaded_file($upfile, $fullloc))
         {
            chmod($fullloc, 0644);
         }
         else
         {
            die("Error moving \"$upfile\" to \"$fullloc\".");
         }
         $test =filesize($fullloc);
      }
      list($original_width, $original_height, $type, $attr) = getimagesize($fullloc);
      list($l_new_width,$l_new_heigth) = calculate_width_height($original_width, $original_height,$lc_width,$lc_height);
      resize($fullloc, $root_dir.$uploaddir."/".$lc_width."_".$lc_height."_".$file_name, $l_new_width, $l_new_heigth);
   $upload_info[] = $lc_message;
   $upload_info[] = $original_width;
   $upload_info[] = $original_height;
   $upload_info[] = $l_new_width;
   $upload_info[] = $l_new_heigth;
   return $upload_info;
   }
?>
<body>
<?php
   $root_dir =$HTTP_SERVER_VARS["DOCUMENT_ROOT"];
   if ($_POST["resize_submit"] == "resize")
   {
      $lc_height           =$_POST["height"];
      $lc_width            =$_POST["width"];
      $day = getdate();
      $minutes = $day['minutes'];
      $temp=gettimeofday();
      $sec=(int)$temp["sec"];
      if (!empty($_FILES["userfile"]["name"]))
      {
         $fileext = substr($_FILES["userfile"]["name"], -4);
         $ffile_name="1_".$minutes."_".$sec.$fileext;
         $tfile_name_tmp=$_FILES["userfile"]["tmp_name"];
         list($lc_upload_message,$original_width,$original_height,$l_new_width,$l_new_height) = upload_file($root_dir,$ffile_name,$tfile_name_tmp,$lc_width,$lc_height);
         if ($lc_upload_message!="")
         {
            echo "error_message".$lc_upload_message;
         }
      }
      ?>
      <form ENCTYPE="multipart/form-data"  action="resize_image.php" method="post">
      <table>
      <tr><td>height              :</td><td><input type="TEXT" name="height" size="10" value="<?php echo $lc_height ; ?>"></td></tr>
      <tr><td>width               :</td><td><input type="TEXT" name="width"  size="10" value="<?php echo $lc_width  ; ?>"></td></tr>
      <tr><td>image to upload     :</td><td><INPUT NAME="userfile" TYPE="file"></td></tr>
      <tr><td><input type="submit" name="resize_submit" value="resize"></td>
      </form>
      </table>
      <table>
      <tr>
      <td><IMG border=0 src="uploads/<?php echo $ffile_name ; ?>" ></td>
      <td  valign=bottom><IMG border=0 src="uploads/<?php echo $lc_width.'_'.$lc_height.'_'.$ffile_name ; ?>" ></td>
      </tr>
      <tr>
      <td><?php echo "org_height:".$original_height." org_width:".$original_width; ?></td>
      <td><?php echo "new_height_".$l_new_height." new_width_".$l_new_width; ?></td>
      </tr>
      </table>
      <?php
   }
   else
   {
      ?>
      <form ENCTYPE="multipart/form-data"  action="resize_image.php" method="post">
      <table>
      <tr><td>height              :</td><td><input type="TEXT" name="height" size="10" ></td></tr>
      <tr><td>width               :</td><td><input type="TEXT" name="width"  size="10" ></td></tr>
      <tr><td>image to upload     :</td><td><INPUT NAME="userfile" TYPE="file"></td></tr>
      <tr><td><input type="submit" name="resize_submit" value="resize"></td>
      </form>
      </table>
      <?php
   }
?>
</body>
</html>
<?php
ob_end_flush();
?>

 

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *