首先,我知道这些关于“memory_limit”错误的问题已经被问了好几次,并且给出的大多数解决方案都涉及 ini_set 修改,但我认为这不是我的情况。
事实上,我确实收到了:[21-Apr-2013 22:21:48 UTC] PHP 致命错误:在 /home/xxx/public_html/www4/admincenter 中允许的内存大小为 52428800 字节已用尽(试图分配 16000 字节) /clientes/102/slides/nuevo_slide.php 第 20 行
如您所见,允许的总内存大小实际上大于函数尝试分配的大小,但我仍然收到相同的错误:\ 据我所知,imagecreatefromjpeg 函数应该有问题(如错误行指出:20)
这是我的代码:顺便说一下,它适用于 img1 而不是 img2:
- img1 = 139kb,1000 x 300 像素
- img2 = 648kb,4000 x 3000 像素
两个大小都在允许的内存之下。我真的很感谢你在这方面的帮助。谢谢!
<?php ini_set("memory_limit", "100M"); ?>
<?php require_once '/home/xxx/private_html/0a/configuration.php';?>
<?php require_once '/home/xxx/private_html/102/configuration.php';?>
<?php
$titulo = ucwords($_POST['titulo']);
$texto = utf8_encode($texto);
$q = "INSERT INTO slides (`id`, `titulo`, `texto`, `fecha`) VALUES (NULL, '".$titulo."', '".$texto."', CURRENT_TIMESTAMP)";
$sql = mysql_query($q);
$id = mysql_insert_id();
for ($i = 1; $i < 3; $i++){
if ($_FILES["foto"]["type"] == "image/jpeg" || $_FILES["foto"]["type"] == "image/pjpeg" ){
if($_FILES["foto"]["type"] == "image/jpeg" || $_FILES["foto"]["type"] == "image/pjpeg"){
$image_source = imagecreatefromjpeg($_FILES["foto"]["tmp_name"]);
}
if ($i == 1){
$max_upload_width = 902;
$max_upload_height = 289;
$remote_file = $sld .$id."_".$max_upload_width."x".$max_upload_height.".jpg";
}
if ($i == 2){
$max_upload_width = 166;
$max_upload_height = 67;
$remote_file = $sld .$id."_".$max_upload_width."x".$max_upload_height.".jpg";
}
imagejpeg($image_source,$remote_file,100);
chmod($remote_file,0755);
// get width and height of original image
list($image_width, $image_height) = getimagesize($remote_file);
if($image_width>$max_upload_width || $image_height >$max_upload_height){
$proportions = $image_width/$image_height;
if($image_width>$image_height){
$new_width = $max_upload_width;
$new_height = round($max_upload_width/$proportions);
}else{
$new_height = $max_upload_height;
$new_width = round($max_upload_height*$proportions);
}
$new_image = imagecreatetruecolor($max_upload_width , $max_upload_height);
$image_source = imagecreatefromjpeg($remote_file);
imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $max_upload_width, $max_upload_height, $image_width, $image_height);
imagejpeg($new_image,$remote_file,100);
imagedestroy($new_image);
}
imagedestroy($image_source);
}
}
header("Location: index.php?msg=350");
exit();
?>