douji6199 2012-03-09 07:59 采纳率: 100%
浏览 15
已采纳

从另一台服务器调整图像大小并将其显示在我的网站中

I wanted to show image from another server, but a smaller image maintaining the aspect ratio and i will not be knowing the exact size of that image. Can i do this?

  • 写回答

1条回答 默认 最新

  • dongze5043 2012-03-09 10:16
    关注

    First you need to grab the image from another server, for that you can use file_get_contents().

    Then you need some code to resize an image. Since you have the image content in a string rather than a local file, you can use imagecreatefromstring() try here: curl and resize remote image

    If you want to resize it down relative to the original then use getimagesizefromstring() to get the current size, then simply calculate the new size based on your chosen percentage.

    $content = file_get_contents('http://site.com/image.jpg');
    
    $originalSize = getimagesizefromstring($content);
    
    $originalWidth = $originalSize[0];
    $originalheight = $originalSize[1];
    
    $newSize = 0.6; // 60% of the original size
    
    $newWidth = $originalWidth * $newSize;
    $newHeight = $originalHeight * $newSize;
    
    // now you know what the resized width and height should be, you can go ahead and do the actual resizing
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?