doumu6941 2014-04-30 07:14
浏览 53
已采纳

如何从宽度为百分比的外部服务器提供图像? (PHP)

We currently use another server (well, the same server but different account and domain) to serve images for our website.

It currently works like this:

http://images.example.com?src=imagepath&w=300&h=500&q=75.

This does automatic image resizing based on the w=300 and h=400 that we provide to the URL (pixels).

However, the site is for a car dealership and we don't always want to specify the width. Sometimes we need percentage widths, as the site is responsive, and also, not all images are the same size to begin with, so it leads to distortion if we say all images are 700 wide.

Is it possible to have this kind of image resizing external server but using percentages?

We use Apache, PHP and the images are accessed via directory names based on criteria in the database for that car, so for example images/ford/focus/AA136YH/image1.jpg.

TL/DR Question: How can we serve images from external PHP server with percentage widths instead of specifying fixed width and/or height?

  • 写回答

3条回答 默认 最新

  • doulianxi0587 2014-04-30 10:03
    关注

    I suggest CSS like the other answers; if you don't want this, use GD Lib but keep in mind this will use some CPU resources everytime the image is requested.

    If you can't use CSS, use a script like the below but cache the scaled image, using Memcached for example or disc storage.

    GD Lib resizing:

    <?php
    $filename = $_GET['src'];
    $percent  = intval($_GET['q']) / 100;
    
    if (!file_exists($filename)) {
        die('image does not exist');
    }
    
    list($width, $height) = getimagesize($filename);
    $newWidth  = $width * $percent;
    $newHeight = $height * $percent;
    
    $scaled = imagecreatetruecolor($newWidth, $newHeight);
    $source = imagecreatefromjpeg($filename);
    
    imagecopyresized($scaled, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    
    header('Content-Type: image/jpeg');
    imagejpeg($scaled);
    imagedestroy($scaled);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型