Problems rotating and resizing large images

ぐ巨炮叔叔 提交于 2019-12-12 03:36:46

问题


After a user has uploaded an image I'm trying to create a thumbnail of it. The problem is that when my script attempts to load a large image into memory in order to resample it (for example taken on an iPad at 2448px x 3264px), it runs out of memory when calling imagecreatefromjpeg. I can't increase the available memory because it's a shared server, and I can't install any alternative image libraries for the same reason.

$name = "upload/1/" . $filename;
move_uploaded_file($_FILES['myFile']['tmp_name'], $name);
createthumb($name, "upload/1/th_" . $filename, 230, 172);

function createthumb($name, $filename, $new_w, $new_h) {
 global $Postcode;

 $system=explode('.', $name);
 if (preg_match('/jpg|jpeg/',$system[1])) {
  $src_img=imagecreatefromjpeg($name);
 }
 if (preg_match('/png/',$system[1])) {
  $src_img=imagecreatefrompng($name);
 }

 $old_x=imageSX($src_img);
 $old_y=imageSY($src_img);
 if ($old_x > $old_y) {
  $thumb_w=$new_w;
  $thumb_h=$old_y*($new_h/$old_x);
 }
 if ($old_x < $old_y) {
  $thumb_w=$old_x*($new_w/$old_y);
  $thumb_h=$new_h;
 }
 if ($old_x == $old_y) {
  $thumb_w=$new_w;
  $thumb_h=$new_h;
 }
 $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
 imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);

 if (preg_match("/png/",$system[1])){
  imagepng($dst_img, $filename); 
 }
 else {
  imagejpeg($dst_img, $filename); 
 }
 imagedestroy($dst_img); 
}

Is there ANY way I can solve this issue with the caveat that I can't increase the memory or use a different image library?

来源:https://stackoverflow.com/questions/34512904/problems-rotating-and-resizing-large-images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!