douqian2334 2012-03-18 12:23
浏览 43
已采纳

调整图像大小(image_lib),保持纵横比,但可以缩放到SMALLEST,超出宽度和高度

So I want to take a square image and resize it to 300px * 400px (for example).

I want to maintain the aspect ratio, but so that the picture exceeds or matches both dimensions. So in the case of a square original image the resulting image will be 400px * 400px.

Does that make sense? I want this because then I will go on to crop the image to be exactly 300px * 400px.

Setting $config['maintain_ratio'] = false; distorts the image.

Thank you for any enlightenment.

  • 写回答

1条回答 默认 最新

  • dousou2897 2012-03-18 12:40
    关注

    Try this .... It will help ..

        if(file_exists('test.jpg'))
             unlink('test.jpg');
    
           $dest='test.jpg';
    
    
      $image='fg.jpg';
    
     $list=getimagesize($image);
     $width=$list[0];
      $height=$list[1];
    
    
       $newwidth=65;
        $newheight=95;
    
    
    
       $int_x=10;
     $int_y=0;
    
    
    
    
    define( 'DESIRED_IMAGE_WIDTH', 65 );
    define( 'DESIRED_IMAGE_HEIGHT', 95 );
    $source_gdim = imagecreatefromjpeg($image);
    
    $source_aspect_ratio = $width / $height;
    $desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT;
    
     // For Wide Image
    if ( $source_aspect_ratio > $desired_aspect_ratio )
      {
    $temp_height = DESIRED_IMAGE_HEIGHT;
    $temp_width = ( int ) ( DESIRED_IMAGE_HEIGHT * $source_aspect_ratio );
      }
      // For Tall Image
    else
      {
      $temp_width = DESIRED_IMAGE_WIDTH;
       $temp_height = ( int ) ( DESIRED_IMAGE_WIDTH / $source_aspect_ratio );
       }
    
    
       $temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
      imagecopyresampled($temp_gdim,$source_gdim,0, 0,0, 0,$temp_width, $temp_height,$width, $height);
    
    
     $x0 = ( $temp_width - DESIRED_IMAGE_WIDTH ) / 2;
      $y0 = ( $temp_height - DESIRED_IMAGE_HEIGHT ) / 2;
    
      $desired_gdim = imagecreatetruecolor( DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT );
        imagecopy($desired_gdim,$temp_gdim,0, 0,$x0, $y0,DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT);
    
    
          imagejpeg( $desired_gdim,'test.jpg',100 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?