Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP上载、调整图像大小和裁剪图像中心_Php_Image_Upload_Resize_Crop - Fatal编程技术网

使用PHP上载、调整图像大小和裁剪图像中心

使用PHP上载、调整图像大小和裁剪图像中心,php,image,upload,resize,crop,Php,Image,Upload,Resize,Crop,我想创建一个非常基本的上载、调整大小和裁剪PHP脚本。 这项功能将与Twitter上传头像图片的方法完全相同(我上次检查过) 我希望脚本拍摄任意大小的图像,将最短的一面调整为116px,然后裁剪顶部和底部(如果是横向的,则裁剪为左侧和右侧),以获得116px乘以116px的正方形 我不想要一个带有客户端大小调整的臃肿PHP脚本,只需要一个简单的PHP大小调整和裁剪。这是如何做到的?GD库是一个很好的起点 如果你想让我的上传、调整大小和裁剪类中的一个例子起作用,那么所有这些加上其他一些很酷的东西—

我想创建一个非常基本的上载、调整大小和裁剪PHP脚本。 这项功能将与Twitter上传头像图片的方法完全相同(我上次检查过)

我希望脚本拍摄任意大小的图像,将最短的一面调整为116px,然后裁剪顶部和底部(如果是横向的,则裁剪为左侧和右侧),以获得116px乘以116px的正方形


我不想要一个带有客户端大小调整的臃肿PHP脚本,只需要一个简单的PHP大小调整和裁剪。这是如何做到的?

GD库是一个很好的起点


如果你想让我的上传、调整大小和裁剪类中的一个例子起作用,那么所有这些加上其他一些很酷的东西——如果需要,你可以全部使用,或者只需去掉你喜欢的部分:

我不认为它太臃肿了您只需执行以下操作(未经测试):

如果((isset($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\==0)){//如果文件已发布,则上载它 include('include_CLASS_FILE_HERE.php'); $myImage=new\u图像; //上传图像 $myImage->uploadTo='uploads/';//在此处设置上载文件夹 $myImage->returnType='array';//返回图像详细信息的数组 $img=$myImage->upload($_FILES['file']); 如果($img){ $myImage->newWidth=116; $myImage->newHeight=116; $i=$myImage->resize();//调整大小至116px,保持纵横比 //获取新的图像高度 $imgWidth=$i['width']; //获取新的图像宽度 $imgHeight=$i['height']; 如果有的话(一美元){ //弄清楚在哪里收割 $cropX=($imgWidth>116)?($imgWidth-116)/2:0; $cropY=($imgHeight>116)?($imgHeight-116)/2:0; $croped=$myImage->cropt(116116,$cropX,$cropY); 如果($crapped){echo'它起作用了(我想!);print_r($crapped); }else{echo'Crop failed';} }else{echo'调整大小失败';} }else{echo'上载失败';}
有一个简单易用的开源库,名为。它使用GD,但将其使用简化为3行

基准使用示例:

$magicianObj = new imageLib('racecar.jpg');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');

我制作了这个简单的函数,非常容易使用,它允许您调整图像大小、裁剪图像并将图像居中到特定的宽度和高度,它可以支持JPG、PNG和GIF。请随意复制并粘贴到您的代码中:

function resize_imagejpg($file, $w, $h, $finaldst) {

   list($width, $height) = getimagesize($file);
   $src = imagecreatefromjpeg($file);
   $ir = $width/$height;
   $fir = $w/$h;
   if($ir >= $fir){
       $newheight = $h; 
       $newwidth = $w * ($width / $height);
   }
   else {
       $newheight = $w / ($width/$height);
       $newwidth = $w;
   }   
   $xcor = 0 - ($newwidth - $w) / 2;
   $ycor = 0 - ($newheight - $h) / 2;


   $dst = imagecreatetruecolor($w, $h);
   imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight, 
   $width, $height);
   imagejpeg($dst, $finaldst);
   imagedestroy($dst);
   return $file;


}






function resize_imagegif($file, $w, $h, $finaldst) {

   list($width, $height) = getimagesize($file);
   $src = imagecreatefromgif($file);
   $ir = $width/$height;
   $fir = $w/$h;
   if($ir >= $fir){
       $newheight = $h; 
       $newwidth = $w * ($width / $height);
   }
   else {
       $newheight = $w / ($width/$height);
       $newwidth = $w;
   }   
   $xcor = 0 - ($newwidth - $w) / 2;
   $ycor = 0 - ($newheight - $h) / 2;


   $dst = imagecreatetruecolor($w, $h);
   $background = imagecolorallocatealpha($dst, 0, 0, 0, 127);
   imagecolortransparent($dst, $background);
   imagealphablending($dst, false);
   imagesavealpha($dst, true);
   imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight, 
   $width, $height);
   imagegif($dst, $finaldst);
   imagedestroy($dst);
   return $file;


}



function resize_imagepng($file, $w, $h, $finaldst) {

   list($width, $height) = getimagesize($file);
   $src = imagecreatefrompng($file);
   $ir = $width/$height;
   $fir = $w/$h;
   if($ir >= $fir){
       $newheight = $h; 
       $newwidth = $w * ($width / $height);
   }
   else {
        $newheight = $w / ($width/$height);
   $newwidth = $w;
   }   
   $xcor = 0 - ($newwidth - $w) / 2;
   $ycor = 0 - ($newheight - $h) / 2;


   $dst = imagecreatetruecolor($w, $h);
   $background = imagecolorallocate($dst, 0, 0, 0);
   imagecolortransparent($dst, $background);
   imagealphablending($dst, false);
   imagesavealpha($dst, true);

   imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, 
   $newheight,$width, $height);

   imagepng($dst, $finaldst);
   imagedestroy($dst);
   return $file;


}








function ImageResize($file, $w, $h, $finaldst) {
      $getsize = getimagesize($file);
      $image_type = $getsize[2];

      if( $image_type == IMAGETYPE_JPEG) {

         resize_imagejpg($file, $w, $h, $finaldst);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         resize_imagegif($file, $w, $h, $finaldst);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         resize_imagepng($file, $w, $h, $finaldst);
      }





}
要使用它,您只需这样称呼它:

ImageResize(image, width, height, destination);
例如


到目前为止,你做了什么?一些脚本?我还没有用这个确切的项目开始它,但我在过去的一些项目中尝试过这样做,但没有弄清楚。我希望有人知道一些基本的代码来让我开始。谢谢-虽然我在我最初的帖子中意识到我遗漏了裁剪方法的最后两个参数-哦!我有我现在修复了上面的问题,希望它能正常运行-以防您需要使用:$myImage->crop($cropToWidth,$cropToHeight,$cropFromX,$cropFromY)调用裁剪方法;这是迄今为止最好的方法。有很多非常易于使用的功能。谢谢!!这对我来说很有效!我可以问一下,为什么那些调整大小的函数都返回空值?
ImageResize(image, width, height, destination);
ImageResize("uploads/face.png", 100, 150, "images/user332profilepic.png");