Jump to content

how to crop an image?


lukelee

Recommended Posts

Looking at the amount of code, I would rather use Image manipulation software... Very interesting though

 

Image manipulation software is useful and you can crop exactly what you want.  The problem is if you are getting uploads from users, how many of them are going to resize to what you require?

Link to comment
https://forums.phpfreaks.com/topic/136475-how-to-crop-an-image/#findComment-712391
Share on other sites

Looking at the amount of code, I would rather use Image manipulation software... Very interesting though

 

Image manipulation software is useful and you can crop exactly what you want.  The problem is if you are getting uploads from users, how many of them are going to resize to what you require?

yeah,I need that function in my cms, thats for other ppl use. I have tryed the codes, but it doesnt work for me.

 

<?php
      cropImage(225, 165, 'mypicture.jpg', 'jpg', 'mypictures.jpg');


      function cropImage($nw, $nh, $source, $stype, $dest) {

       

          $size = getimagesize($source);

          $w = $size[0];

          $h = $size[1];

       

          switch($stype) {

              case 'gif':

              $simg = imagecreatefromgif($source);

              break;

              case 'jpg':

              $simg = imagecreatefromjpeg($source);

              break;
  16.
              case 'png':

              $simg = imagecreatefrompng($source);

    break;

          }

       

          $dimg = imagecreatetruecolor($nw, $nh);

       
$wm = $w/$nw;

          $hm = $h/$nh;

       

          $h_height = $nh/2;

          $w_height = $nw/2;

       

          if($w> $h) {

       

              $adjusted_width = $w / $hm;

              $half_width = $adjusted_width / 2;

              $int_width = $half_width - $w_height;

       

              imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);

       

          } elseif(($w <$h) || ($w == $h)) {

       

              $adjusted_height = $h / $wm;

              $half_height = $adjusted_height / 2;

              $int_height = $half_height - $h_height;

       

imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
       

} else {

    imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);

}

       

imagejpeg($dimg,$dest,100);

}
?>

Link to comment
https://forums.phpfreaks.com/topic/136475-how-to-crop-an-image/#findComment-712430
Share on other sites

Looking at the amount of code, I would rather use Image manipulation software... Very interesting though

 

Image manipulation software is useful and you can crop exactly what you want.  The problem is if you are getting uploads from users, how many of them are going to resize to what you require?

yeah,I need that function in my cms, thats for other ppl use. I have tryed the codes, but it doesnt work for me.

 

<?php
      cropImage(225, 165, 'mypicture.jpg', 'jpg', 'mypictures.jpg');


      function cropImage($nw, $nh, $source, $stype, $dest) {

       

          $size = getimagesize($source);

          $w = $size[0];

          $h = $size[1];

       

          switch($stype) {

              case 'gif':

              $simg = imagecreatefromgif($source);

              break;

              case 'jpg':

              $simg = imagecreatefromjpeg($source);

              break;
  16.
              case 'png':

              $simg = imagecreatefrompng($source);

    break;

          }

       

          $dimg = imagecreatetruecolor($nw, $nh);

       
$wm = $w/$nw;

          $hm = $h/$nh;

       

          $h_height = $nh/2;

          $w_height = $nw/2;

       

          if($w> $h) {

       

              $adjusted_width = $w / $hm;

              $half_width = $adjusted_width / 2;

              $int_width = $half_width - $w_height;

       

              imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);

       

          } elseif(($w <$h) || ($w == $h)) {

       

              $adjusted_height = $h / $wm;

              $half_height = $adjusted_height / 2;

              $int_height = $half_height - $h_height;

       

imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
       

} else {

    imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);

}

       

imagejpeg($dimg,$dest,100);

}
?>

 

First, you need to change the size in the function call, and second make sure that you're path to the image is correct.

Link to comment
https://forums.phpfreaks.com/topic/136475-how-to-crop-an-image/#findComment-713623
Share on other sites

finally, I have found a code which is working for me, but some of them I dont unstand, it gives a new name for thumbnail image in function dst_img() which is within resizeimg class. how can i display it on the screen after echo $image1; ?here is the code:

 

<?php

class resizeimage
{
        var $type;
        var $width;
        var $height;
        var $resize_width;
        var $resize_height;
        var $cut;
        var $srcimg;
        var $dstimg;
        var $im;

        function resizeimage($img, $wid, $hei,$c)
        {
                $this->srcimg = $img;
                $this->resize_width = $wid;
                $this->resize_height = $hei;
                $this->cut = $c;

                $this->type = substr(strrchr($this->srcimg,"."),1);

                $this->initi_img();
    
                $this -> dst_img();

                $this->width = imagesx($this->im);
                $this->height = imagesy($this->im);
        
                $this->newimg();
                ImageDestroy ($this->im);
        }
        function newimg()
        {

              
                $resize_ratio = ($this->resize_width)/($this->resize_height);

                $ratio = ($this->width)/($this->height);

                {
                        if($ratio>=$resize_ratio)

                        {

                                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);

                                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);


                                ImageJpeg ($newimg,$this->dstimg);
                                echo "thumbnail created";
                        
                        }
                        if($ratio<$resize_ratio)

                        {
                                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));


                                ImageJpeg ($newimg,$this->dstimg);
                                echo "thumbnail created";
                        
                        }

                }
                else
                {
                        if($ratio>=$resize_ratio)
                        {
                                $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
                                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);



                                ImageJpeg ($newimg,$this->dstimg);
                                echo "thumbnail created";
                        
                        }
                        if($ratio<$resize_ratio)
                        {
                                $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
                                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);



                                ImageJpeg ($newimg,$this->dstimg);
                                echo "thumbnail created";
                        
                        }
                }

         }

       
        function initi_img()
        {
                if($this->type=="jpg")
                {
                        $this->im = imagecreatefromjpeg($this->srcimg);
                }
                if($this->type=="gif")
                {
                        $this->im = imagecreatefromgif($this->srcimg);
                }
                if($this->type=="png")
                {
                        $this->im = imagecreatefrompng($this->srcimg);
                }
        }
        
        function dst_img()
        {
                $full_length  = strlen($this->srcimg);
                $type_length  = strlen($this->type);
                $name_length  = $full_length-$type_length;
                $name         = substr($this->srcimg,0,$name_length-1);
                $this->dstimg = $name."_small.".$this->type;
            
        }
}

$tempimgname = strtolower($_FILES["file"][name]);

$tempimgname = mb_convert_encoding(   $tempimgname,   "gb2312",   "utf-8");

$tmpfiletype = substr(strrchr($tempimgname,"."),1);


if($tmpfiletype=="jpg" || $tmpfiletype=="gif" || $tmpfiletype=="png")
{
        if(copy($_FILES["file"]["tmp_name"],strtolower("image/".date("Y-m-d-h-m-s",time()).$tempimgname)))
        {
          $image1 = basename(strtolower("image/".date("Y-m-d-h-m-s",time()).$tempimgname));
         $class = new resizeimage("image/".date("Y-m-d-h-m-s",time()).$tempimgname, 150, 150, 1);

            echo "file".strtolower($_FILES["file"]["name"])."<br>"."大小".$_FILES ["file"]["size"]."<br>"."type is".$_FILES["file"]["type"]."< br>"."success"."<br>";
         
         echo $image1;
        }
        else
        {
            echo "failed"."<br>";
        }
    
}
else
{
    echo "wrong image type;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/136475-how-to-crop-an-image/#findComment-713783
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.