Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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 Processing_Scale_Image Resizing - Fatal编程技术网

如何使用PHP缩小服务器端的图像?

如何使用PHP缩小服务器端的图像?,php,image-processing,scale,image-resizing,Php,Image Processing,Scale,Image Resizing,我有一些从服务器提取的图像,$imgUrl保存图像的路径 现在,我使用“width=“100”height=“200”/>或CSS来缩小图像的比例,但我想用PHP来实现这一点,以便将已缩放的图像提供给DOM 有什么想法吗 感谢请注意,在PHP中执行此操作意味着每次访问图像(如果在运行中执行)或保存图像时(这意味着您将使用更多存储来保存转换后的图像)都需要大量内存。如果您仍然确定这是您需要/想要的内容,请查看使用方法。请参阅此答案以了解相关想法或方法:您应该创建一个较小的版本并将其保存到文件夹中。

我有一些从服务器提取的图像,
$imgUrl
保存图像的路径

现在,我使用
“width=“100”height=“200”/>
或CSS来缩小图像的比例,但我想用PHP来实现这一点,以便将已缩放的图像提供给DOM

有什么想法吗


感谢

请注意,在PHP中执行此操作意味着每次访问图像(如果在运行中执行)或保存图像时(这意味着您将使用更多存储来保存转换后的图像)都需要大量内存。如果您仍然确定这是您需要/想要的内容,请查看使用方法。请参阅此答案以了解相关想法或方法:

您应该创建一个较小的版本并将其保存到文件夹中。这样,您就不必在每次请求时都对其进行重新压缩(这需要大量内存)。使用Gd或ImageMagick调整大小


此解决方案将在首次请求拇指时创建拇指。所有未来的请求都将获取已创建的拇指。它正在使用:

HTML:


以下是我正在使用的atm。请随时提取您需要的信息:

 Usage:
 <CLASS>::scale_image($dir.$name, 1024, 768, $dir.'thumb_'.$name);

 /**
 * Most simple way to get File Extension
 * @param string $path Local path to File
 * @return string Extension in lower case
 */
public static function extension($path){
    return strtolower(pathinfo($path, PATHINFO_EXTENSION));
}
/**
 * Rename Filename for more secure usage
 * @param string $name filename
 * @return string more secure filename
 */
public static function secure_name($name){
    return urlencode(preg_replace('/[^a-z0-9 \-_\.]/i', '_', strtolower($name)));
}
/**
 * Scale Image without ratio changes
 * @param int $sw source width of orig image
 * @param int $sh source height of orig image
 * @param int $tw max target width
 * @param int $th max target height
 * @return array list($width, $height)
 */
public static function scale($sw, $sh, $tw, $th){
    if ($sw > $tw && $sw/$tw > $sh/$th) {
        $tw = $sw * ($tw / $sw); 
        $th = $sh * ($tw / $sw); 
    }else if ($sh > $th) { 
        $tw = $sw * ($th / $sh); 
        $th = $sh * ($th / $sh); 
    }else{
        $th = $sh;
        $tw = $sw;
    }
    return array(round($tw), round($th));
}
/**
 * Scale Image 
 * @param string $sPath
 * @param int $width max width
 * @param int $height max height
 * @param string $tPath Optional Path for Thumbnail
 * @param int $thumbScale Scale ratio for optional Thumbnail (Width and Height / $thumbScale)
 * @return void
 */
public static function scale_image($sPath, $width, $height, $tPath = NULL, $thumbScale = 10){
    if(!function_exists('imagecreatetruecolor')){
        return;
    }
    $ext = strtolower(self::extension($sPath));
    if($ext == 'jpg' or $ext == 'jpeg'){
        $src = imagecreatefromjpeg($sPath);
    }elseif($ext == 'png'){
        $src = imagecreatefrompng($sPath);
    }elseif($ext == 'gif'){
         $src = imagecreatefromgif($sPath);
    }else{
        return;
    }
    list($sw, $sh) = getimagesize($sPath);
    list($tw, $th) = File::scale($sw, $sh, $width, $height);
    $trg = imagecreatetruecolor($tw, $th);
    imagecopyresampled($trg, $src, 0, 0, 0, 0, $tw, $th, $sw, $sh);
    imagejpeg($trg, $sPath, 90);
    if($tPath){
        $tw = (int)$tw / $thumbScale;
        $th = (int)$th / $thumbScale;
        $trg = imagecreatetruecolor($tw, $th);
        imagecopyresampled($trg, $src, 0, 0, 0, 0, $tw, $th, $sw, $sh);
        imagejpeg($trg, $tPath, 90);
    }
}
用法:
::缩放图像($dir.$name,1024768,$dir.$thumb.$name);
/**
*获取文件扩展名的最简单方法
*@param string$path文件的本地路径
*@小写返回字符串扩展名
*/
公共静态函数扩展($path){
返回strtolower(pathinfo($path,pathinfo_扩展名));
}
/**
*重命名文件名以获得更安全的使用
*@param string$name文件名
*@returnstring更安全的文件名
*/
公共静态函数secure_name($name){
返回urlencode(preg_replace('/[^a-z0-9-\.]/i',''''''''u',strtolower($name));
}
/**
*缩放图像而不改变比率
*@param int$sw原始图像的源宽度
*@param int$sh原始图像的源高度
*@param int$tw最大目标宽度
*@param int$th最大目标高度
*@return数组列表($width,$height)
*/
公共静态功能量表($sw、$sh、$tw、$th){
如果($sw>$tw&&$sw/$tw>$sh/$th){
$tw=$sw*($tw/$sw);
$th=$sh*($tw/$sw);
}如果($sh>$th){
$tw=$sw*($th/$sh);
$th=$sh*($th/$sh);
}否则{
$th=$sh;
$tw=$sw;
}
返回数组(圆形($tw),圆形($th));
}
/**
*缩放图像
*@param string$sPath
*@param int$width最大宽度
*@param int$height最大高度
*@param string$tPath缩略图的可选路径
*@param int$thumbScale可选缩略图的比例(宽度和高度/$thumbScale)
*@返回无效
*/
公共静态函数缩放图像($sPath、$width、$height、$tPath=NULL、$thumbScale=10){
如果(!函数_存在('ImageCreateTureColor')){
返回;
}
$ext=strtolower(self::extension($sPath));
如果($ext=='jpg'或$ext=='jpeg'){
$src=imagecreatefromjpeg($sPath);
}elseif($ext=='png'){
$src=imagecreatefrompng($sPath);
}elseif($ext==“gif”){
$src=imagecreatefromformgif($sPath);
}否则{
返回;
}
列表($sw,$sh)=getimagesize($sPath);
列表($tw,$th)=文件::比例($sw,$sh,$width,$height);
$trg=imagecreatetruecolor($tw,$th);
imagecopyresampled($trg、$src、0、0、0、$tw、$th、$sw、$sh);
图像JPEG($trg,$sPath,90);
若有($tPath){
$tw=(int)$tw/$thumbScale;
$th=(int)$th/$thumbScale;
$trg=imagecreatetruecolor($tw,$th);
imagecopyresampled($trg、$src、0、0、0、$tw、$th、$sw、$sh);
图像JPEG($trg,$tPath,90);
}
}

像phpthumb这样最容易使用的工具:确保在请求页面之前执行此操作,而不是按需操作,因为按需操作会使页面执行速度变慢,从而使您的网站速度变慢。@evan如果您回答了,我会投赞成票。除非OP真的想编写自己的图像处理库,我认为这在php中会很痛苦@Tim写一些适合他需要的东西(不是一个完整的库,只是一个重定大小器)其实并不像听起来那么难。是的,Tim是wright,我要求为重定大小器编写代码。我知道所有这些库,但我想GD是我的选择。我有很多,我的意思是很多图像,我宁愿花一些内存,然后修改现有的代码
$width  = 140;
$height = 80;
$image  = $_GET['img'];
$ext    = 'png';

// Check if file exists
if ( ! file_exists('/path/to/the/'.$image.'.'.$ext))
{
    die('Unable to process the requested file.');
}

// Check if a thumb already exists, otherwise create a thumb
if (file_exists('/path/to/the/'.$image.'_thumb.'.$ext))
{
    $img = new imagick('/path/to/the/'.$image.'_thumb.'.$ext);
}
else
{
    $img = new imagick('/path/to/the/'.$image.'.'.$ext);
    $img->setImageFormat($ext);
    $img->scaleImage($width, 0);
    $img->cropImage($width, $height, 0, 0);
    $img->writeImage('/path/to/the/'.$image.'_thumb.'.$ext);
}

// Return as an image
header('Content-Type: image/'.$ext);
echo $img;
 Usage:
 <CLASS>::scale_image($dir.$name, 1024, 768, $dir.'thumb_'.$name);

 /**
 * Most simple way to get File Extension
 * @param string $path Local path to File
 * @return string Extension in lower case
 */
public static function extension($path){
    return strtolower(pathinfo($path, PATHINFO_EXTENSION));
}
/**
 * Rename Filename for more secure usage
 * @param string $name filename
 * @return string more secure filename
 */
public static function secure_name($name){
    return urlencode(preg_replace('/[^a-z0-9 \-_\.]/i', '_', strtolower($name)));
}
/**
 * Scale Image without ratio changes
 * @param int $sw source width of orig image
 * @param int $sh source height of orig image
 * @param int $tw max target width
 * @param int $th max target height
 * @return array list($width, $height)
 */
public static function scale($sw, $sh, $tw, $th){
    if ($sw > $tw && $sw/$tw > $sh/$th) {
        $tw = $sw * ($tw / $sw); 
        $th = $sh * ($tw / $sw); 
    }else if ($sh > $th) { 
        $tw = $sw * ($th / $sh); 
        $th = $sh * ($th / $sh); 
    }else{
        $th = $sh;
        $tw = $sw;
    }
    return array(round($tw), round($th));
}
/**
 * Scale Image 
 * @param string $sPath
 * @param int $width max width
 * @param int $height max height
 * @param string $tPath Optional Path for Thumbnail
 * @param int $thumbScale Scale ratio for optional Thumbnail (Width and Height / $thumbScale)
 * @return void
 */
public static function scale_image($sPath, $width, $height, $tPath = NULL, $thumbScale = 10){
    if(!function_exists('imagecreatetruecolor')){
        return;
    }
    $ext = strtolower(self::extension($sPath));
    if($ext == 'jpg' or $ext == 'jpeg'){
        $src = imagecreatefromjpeg($sPath);
    }elseif($ext == 'png'){
        $src = imagecreatefrompng($sPath);
    }elseif($ext == 'gif'){
         $src = imagecreatefromgif($sPath);
    }else{
        return;
    }
    list($sw, $sh) = getimagesize($sPath);
    list($tw, $th) = File::scale($sw, $sh, $width, $height);
    $trg = imagecreatetruecolor($tw, $th);
    imagecopyresampled($trg, $src, 0, 0, 0, 0, $tw, $th, $sw, $sh);
    imagejpeg($trg, $sPath, 90);
    if($tPath){
        $tw = (int)$tw / $thumbScale;
        $th = (int)$th / $thumbScale;
        $trg = imagecreatetruecolor($tw, $th);
        imagecopyresampled($trg, $src, 0, 0, 0, 0, $tw, $th, $sw, $sh);
        imagejpeg($trg, $tPath, 90);
    }
}