3
I’m using a simple script to crop the image, while cropping some images it gives this error:
Allowed memory size of 268435456 bytes exhausted (tried to allocate 155648 bytes) in /home/site/public_html/admin/controller/photo.php on line 65
The strange thing is that the image is only 65kb, does anyone know what it might be? There’s a way I can generate a more detailed log?
below the script and the commented line 65:
//RECORTAR E IMPORTAR AS FOTOS NO FTP ****************************************
foreach($_FILES as $key => $ft){
if($ft['tmp_name']!=''){
$pastafoto = $root.'imagens/usuarios/';
$photo_url = $ft['tmp_name'];
list($width, $height, $type, $attr) = getimagesize($photo_url);
$nomearquivo=$ft['name'];
if($key=='foto'){
$proporcao=1;
$tamanho='150';
}elseif($key=='capa'){
$tamanho='1060';
$proporcao=6.057142857142857;
}
if(!is_dir($pastafoto.$usuario['id'])){
mkdir($pastafoto.$usuario['id'], 0755);
}
$dest = $pastafoto.$usuario['id'].'/' . $nomearquivo;
$source_image = $photo_url;
$nome=$nomearquivo;
$destination = $pastafoto.$usuario['id'].'/'.$local.'/'.$nome;
$tn_w = $tamanho;
$tn_h = ($tamanho * $proporcao);
$quality = 80;
$wmsource = '';
$success = image_handler($source_image,$destination,$tn_w,$tn_h,$quality,$wmsource);
$sql = "UPDATE ptp_usuarios SET {$key}='{$nome}' WHERE id = {$usuario['id']}";
$result = $db->prepare( $sql );
$result->execute();
}
}
//FUNÇÃO COPIA FOTO
function image_handler($source_image,$destination,$tn_w = 100,$tn_h = 100,$quality = 100,$wmsource = false) {
$info = getimagesize($source_image);
$imgtype = image_type_to_mime_type($info[2]);
switch ($imgtype) {
case 'image/jpeg':
$source = imagecreatefromjpeg($source_image);
break;
case 'image/gif':
$source = imagecreatefromgif($source_image);
break;
case 'image/png':
$source = imagecreatefrompng($source_image);
break;
default:
die('Invalid image type.');
}
$src_w = imagesx($source);
$src_h = imagesy($source);
$src_ratio = $src_w/$src_h;
if ($tn_w/$tn_h > $src_ratio) {
$new_h = $tn_w/$src_ratio;
$new_w = $tn_w;
} else {
$new_w = $tn_h*$src_ratio;
$new_h = $tn_h;
}
$x_mid = $new_w/2;
$y_mid = $new_h/2;
// Now actually apply the crop and resize!
//ESSA LINHA ABAIXO É A QUE FALA QUE DEU ERRO! ***********************************************
$newpic = imagecreatetruecolor(round($new_w), round($new_h));
imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
$final = imagecreatetruecolor($tn_w, $tn_h);
imagecopyresampled($final, $newpic, 0, 0, ($x_mid-($tn_w/2)), ($y_mid-($tn_h/2)), $tn_w, $tn_h, $tn_w, $tn_h);
// If a watermark source file is specified, get the information about the watermark as well. This is the same thing we did above for the source image.
if($wmsource) {
$info = getimagesize($wmsource);
$imgtype = image_type_to_mime_type($info[2]);
switch ($imgtype) {
case 'image/jpeg':
$watermark = imagecreatefromjpeg($wmsource);
break;
case 'image/gif':
$watermark = imagecreatefromgif($wmsource);
break;
case 'image/png':
$watermark = imagecreatefrompng($wmsource);
break;
default:
die('Invalid watermark type.');
}
// Determine the size of the watermark, because we're going to specify the placement from the top left corner of the watermark image, so the width and height of the watermark matter.
$wm_w = imagesx($watermark);
$wm_h = imagesy($watermark);
// Now, figure out the values to place the watermark in the bottom right hand corner. You could set one or both of the variables to "0" to watermark the opposite corners, or do your own math to put it somewhere else.
//$wm_x = $tn_w - $wm_w;
//$wm_y = $tn_h - $wm_h;
$wm_x = ($tn_w / 2) - ($wm_w / 2);
$wm_y = ($tn_h / 2) - ($wm_h / 2);
// Copy the watermark onto the original image
// The last 4 arguments just mean to copy the entire watermark
imagecopy($final, $watermark, $wm_x, $wm_y, 0, 0, $tn_w, $tn_h);
}
// Ok, save the output as a jpeg, to the specified destination path at the desired quality.
// You could use imagepng or imagegif here if you wanted to output those file types instead.
if(Imagejpeg($final,$destination,$quality)) {
return true;
}
// If something went wrong
return false;
}
the answer helped you? If yes, try to schedule it to help other colleagues.
– user148170