php - Image from URL then crop -


so i've been trying grab image external url, crop , save it. copy , save okay it's crop part troubling me. can't figure out how image resource curl stuff (i'm no curl else's curl stuff).

i though this:

$img = imagecreatefromstring($image); $crop = imagecreatetruecolor(8,8); imagecopy ( $crop, $img, 0, 0, 8, 8, 8, 8); 

but no luck there, saves corrupt png. here full code:

            $link = "urlhere";         $path = './mcimages/faces/';              $curl_handle=curl_init(urldecode($link));             curl_setopt($curl_handle, curlopt_nobody, true);             $result = curl_exec($curl_handle);             $retcode = false;             if($result !== false)             {                 $status = curl_getinfo($curl_handle, curlinfo_http_code);                 if($status == 200)                     $retcode = true;             }             curl_close($curl_handle);              if($retcode)             {                 $curl_handle=curl_init();                 curl_setopt($curl_handle,curlopt_url,urldecode($link));                 curl_setopt($curl_handle,curlopt_connecttimeout,2);                 curl_setopt($curl_handle,curlopt_returntransfer,1);                 $image = curl_exec($curl_handle);                 curl_close($curl_handle);                  if($image !== false)                 {                     $img = imagecreatefromstring($image);                     $crop = imagecreatetruecolor(8,8);                      imagecopy ( $crop, $img, 0, 0, 8, 8, 8, 8 );                      if(strpos($link,"/") !== false)                      {                         $name = explode("/",$link);                         $total = count($name);                         $handle = fopen($path.$name[$total-1],"w") or die("could not create : ".$path.rand()."_".$name[$total-1]);                         if($handle !== false)                         {                             fwrite($handle,$crop);                             fclose($handle);                             echo 'the file has been saved !';                          }                     }                 }             } else {                 echo 'file not found !';             } 

afaik, wrong:

fwrite($handle,$crop); 

use

imagejpeg($crop, 'output-file.jpg'); // or imagepng() 

your $crop resource, not binary string image data.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -