创建动态图像调整大小后重定向到二进制图像文件
我在 php 中有一个图像调整大小脚本,它在传递图像路径、高度和宽度参数时返回调整大小后的图像路径。我的问题是我不想要图像的 url,而是实际重定向到以 .jpg 或 png 结尾的特定图像路径(无论适用什么)。
调整大小功能就在这里。
<?php
function resize($imagePath,$opts=null){
# start configuration
$cacheFolder = './cache/'; # path to your cache folder, must be writeable by web server
$remoteFolder = $cacheFolder.'remote/'; # path to the folder you wish to download remote images into
$quality = 90; # image quality to use for ImageMagick (0 - 100)
$cache_http_minutes = 20; # cache downloaded http images 20 minutes
$path_to_convert = 'convert'; # this could be something like /usr/bin/convert or /opt/local/share/bin/convert
## you shouldn't need to configure anything else beyond this point
$purl = parse_url($imagePath);
$finfo = pathinfo($imagePath);
$ext = $finfo['extension'];
# check for remote image..
if(isset($purl['scheme']) && $purl['scheme'] == 'http'):
# grab the image, and cache it so we have something to work with..
list($filename) = explode('?',$finfo['basename']);
$local_filepath = $remoteFolder.$filename;
$download_image = true;
if(file_exists($local_filepath)):
if(filemtime($local_filepath) < strtotime('+'.$cache_http_minutes.' minutes')):
$download_image = false;
endif;
endif;
if($download_image == true):
$img = file_get_contents($imagePath);
file_put_contents($local_filepath,$img);
endif;
$imagePath = $local_filepath;
endif;
if(file_exists($imagePath) == false):
$imagePath = $_SERVER['DOCUMENT_ROOT'].$imagePath;
if(file_exists($imagePath) == false):
return 'image not found';
endif;
endif;
if(isset($opts['w'])): $w = $opts['w']; endif;
if(isset($opts['h'])): $h = $opts['h']; endif;
$filename = md5_file($imagePath);
if(!empty($w) and !empty($h)):
$newPath = $cacheFolder.$filename.'_w'.$w.'_h'.$h.(isset($opts['crop']) && $opts['crop'] == true ? "_cp" : "").(isset($opts['scale']) && $opts['scale'] == true ? "_sc" : "").'.'.$ext;
elseif(!empty($w)):
$newPath = $cacheFolder.$filename.'_w'.$w.'.'.$ext;
elseif(!empty($h)):
$newPath = $cacheFolder.$filename.'_h'.$h.'.'.$ext;
else:
return false;
endif;
$create = true;
if(file_exists($newPath) == true):
$create = false;
$origFileTime = date("YmdHis",filemtime($imagePath));
$newFileTime = date("YmdHis",filemtime($newPath));
if($newFileTime < $origFileTime):
$create = true;
endif;
endif;
if($create == true):
if(!empty($w) and !empty($h)):
list($width,$height) = getimagesize($imagePath);
$resize = $w;
if($width > $height):
$resize = $w;
if(isset($opts['crop']) && $opts['crop'] == true):
$resize = "x".$h;
endif;
else:
$resize = "x".$h;
if(isset($opts['crop']) && $opts['crop'] == true):
$resize = $w;
endif;
endif;
if(isset($opts['scale']) && $opts['scale'] == true):
$cmd = $path_to_convert." ".$imagePath." -resize ".$resize." -quality ".$quality." ".$newPath;
else:
$cmd = $path_to_convert." ".$imagePath." -resize ".$resize." -size ".$w."x".$h." xc:".(isset($opts['canvas-color'])?$opts['canvas-color']:"transparent")." +swap -gravity center -composite -quality ".$quality." ".$newPath;
endif;
else:
$cmd = $path_to_convert." ".$imagePath." -thumbnail ".(!empty($h) ? 'x':'').$w."".(isset($opts['maxOnly']) && $opts['maxOnly'] == true ? "\>" : "")." -quality ".$quality." ".$newPath;
endif;
$c = exec($cmd);
endif;
# return cache file path
return str_replace($_SERVER['DOCUMENT_ROOT'],'',$newPath);
}
?>
$newpath 返回图像的实际路径,但我想重定向到返回的图像路径,即 http://localhost/myfolder /image.jpg 。所以如果我传递一个参数,它应该将我重定向到二进制文件图像路径。到目前为止,我已经尝试使用 header("Location: echo $newpath");但它在 url 上显示 $newPath。帮助!!
i have an image re sizing script in php which returns the image path after been resized when passed the image path,height and width parameters. My problem is i dont want url of the image but to actually redirect to that particular image path ending in .jpg or png whatever is applicable.
Resizing function is here.
<?php
function resize($imagePath,$opts=null){
# start configuration
$cacheFolder = './cache/'; # path to your cache folder, must be writeable by web server
$remoteFolder = $cacheFolder.'remote/'; # path to the folder you wish to download remote images into
$quality = 90; # image quality to use for ImageMagick (0 - 100)
$cache_http_minutes = 20; # cache downloaded http images 20 minutes
$path_to_convert = 'convert'; # this could be something like /usr/bin/convert or /opt/local/share/bin/convert
## you shouldn't need to configure anything else beyond this point
$purl = parse_url($imagePath);
$finfo = pathinfo($imagePath);
$ext = $finfo['extension'];
# check for remote image..
if(isset($purl['scheme']) && $purl['scheme'] == 'http'):
# grab the image, and cache it so we have something to work with..
list($filename) = explode('?',$finfo['basename']);
$local_filepath = $remoteFolder.$filename;
$download_image = true;
if(file_exists($local_filepath)):
if(filemtime($local_filepath) < strtotime('+'.$cache_http_minutes.' minutes')):
$download_image = false;
endif;
endif;
if($download_image == true):
$img = file_get_contents($imagePath);
file_put_contents($local_filepath,$img);
endif;
$imagePath = $local_filepath;
endif;
if(file_exists($imagePath) == false):
$imagePath = $_SERVER['DOCUMENT_ROOT'].$imagePath;
if(file_exists($imagePath) == false):
return 'image not found';
endif;
endif;
if(isset($opts['w'])): $w = $opts['w']; endif;
if(isset($opts['h'])): $h = $opts['h']; endif;
$filename = md5_file($imagePath);
if(!empty($w) and !empty($h)):
$newPath = $cacheFolder.$filename.'_w'.$w.'_h'.$h.(isset($opts['crop']) && $opts['crop'] == true ? "_cp" : "").(isset($opts['scale']) && $opts['scale'] == true ? "_sc" : "").'.'.$ext;
elseif(!empty($w)):
$newPath = $cacheFolder.$filename.'_w'.$w.'.'.$ext;
elseif(!empty($h)):
$newPath = $cacheFolder.$filename.'_h'.$h.'.'.$ext;
else:
return false;
endif;
$create = true;
if(file_exists($newPath) == true):
$create = false;
$origFileTime = date("YmdHis",filemtime($imagePath));
$newFileTime = date("YmdHis",filemtime($newPath));
if($newFileTime < $origFileTime):
$create = true;
endif;
endif;
if($create == true):
if(!empty($w) and !empty($h)):
list($width,$height) = getimagesize($imagePath);
$resize = $w;
if($width > $height):
$resize = $w;
if(isset($opts['crop']) && $opts['crop'] == true):
$resize = "x".$h;
endif;
else:
$resize = "x".$h;
if(isset($opts['crop']) && $opts['crop'] == true):
$resize = $w;
endif;
endif;
if(isset($opts['scale']) && $opts['scale'] == true):
$cmd = $path_to_convert." ".$imagePath." -resize ".$resize." -quality ".$quality." ".$newPath;
else:
$cmd = $path_to_convert." ".$imagePath." -resize ".$resize." -size ".$w."x".$h." xc:".(isset($opts['canvas-color'])?$opts['canvas-color']:"transparent")." +swap -gravity center -composite -quality ".$quality." ".$newPath;
endif;
else:
$cmd = $path_to_convert." ".$imagePath." -thumbnail ".(!empty($h) ? 'x':'').$w."".(isset($opts['maxOnly']) && $opts['maxOnly'] == true ? "\>" : "")." -quality ".$quality." ".$newPath;
endif;
$c = exec($cmd);
endif;
# return cache file path
return str_replace($_SERVER['DOCUMENT_ROOT'],'',$newPath);
}
?>
$newpath returns the actual path of an image but i want to redirect to the returned image path i.e http://localhost/myfolder/image.jpg .so if i pass a parameter it should redirect me to the binary file image path.So far i have tried using header("Location: echo $newpath"); but its showing $newPath on the url. Help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我相信它会是
header("Location: $newpath")
。如果这不起作用,请尝试header('Location: '.$newpath);
。查看此处了解更多信息。
I believe it would be
header("Location: $newpath")
. If that doesn't work, tryheader('Location: '.$newpath);
.Look here for more information.