summaryrefslogtreecommitdiff
path: root/inc/media.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/media.php')
-rw-r--r--inc/media.php34
1 files changed, 34 insertions, 0 deletions
diff --git a/inc/media.php b/inc/media.php
index 2268ad877..e29a47631 100644
--- a/inc/media.php
+++ b/inc/media.php
@@ -1795,6 +1795,9 @@ function media_resize_image($file, $ext, $w, $h=0){
// we wont scale up to infinity
if($w > 2000 || $h > 2000) return $file;
+ // resize necessary? - (w,h) = native dimensions
+ if(($w == $info[0]) && ($h == $info[1])) return $file;
+
//cache
$local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext);
$mtime = @filemtime($local); // 0 if not exists
@@ -1828,6 +1831,13 @@ function media_crop_image($file, $ext, $w, $h=0){
// calculate crop size
$fr = $info[0]/$info[1];
$tr = $w/$h;
+
+ // check if the crop can be handled completely by resize,
+ // i.e. the specified width & height match the aspect ratio of the source image
+ if ($w == round($h*$fr)) {
+ return media_resize_image($file, $ext, $w);
+ }
+
if($tr >= 1){
if($tr > $fr){
$cw = $info[0];
@@ -1865,6 +1875,30 @@ function media_crop_image($file, $ext, $w, $h=0){
}
/**
+ * Calculate a token to be used to verify fetch requests for resized or
+ * cropped images have been internally generated - and prevent external
+ * DDOS attacks via fetch
+ *
+ * @param string $id id of the image
+ * @param int $w resize/crop width
+ * @param int $h resize/crop height
+ *
+ * @author Christopher Smith <chris@jalakai.co.uk>
+ */
+function media_get_token($id,$w,$h){
+ // token is only required for modified images
+ if ($w || $h) {
+ $token = auth_cookiesalt().$id;
+ if ($w) $token .= '.'.$w;
+ if ($h) $token .= '.'.$h;
+
+ return substr(md5($token),0,6);
+ }
+
+ return '';
+}
+
+/**
* Download a remote file and return local filename
*
* returns false if download fails. Uses cached file if available and