redimensionamento de imagens a partir de um banco de dados com PHP
-
13-09-2019 - |
Pergunta
Eu tenho atualmente um script que vai puxar a conteúdo da imagem a partir de um banco de dados e, usando createimagefromstring (), retorna uma imagem redimensionada (resampled) para exibição:
$max_width = 170;
// Get the image
@mysql_connect('host', 'user', 'pass');
@mysql_select_db('db');
$query = "SELECT `thumbnail_contents`, `thumbnail_mimetype` FROM `videos` WHERE video_number = '$video_number'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_object($result);
// Resize if pic bigger than $max_width
// Set the header
header('Content-type:' . $row->thumbnail_mimetype);
// Get original size of image
$image = imagecreatefromstring($row->thumbnail_contents);
$current_width = imagesx($image);
$current_height = imagesy($image);
// Set thumbnail width
$widths = array($current_width, $max_width);
$new_width = min($widths);
// Calculate thumbnail height from given width to maintain ratio
$new_height = $current_height / $current_width*$new_width;
// Create new image using thumbnail sizes
$thumb = imagecreatetruecolor($new_width,$new_height);
// Copy original image to thumbnail
imagecopyresampled($thumb,$image,0,0,0,0,$new_width,$new_height,imagesx($image),imagesy($image));
// Show thumbnail on screen
$show = imagejpeg($thumb);
// Clean memory
imagedestroy($image);
imagedestroy($thumb);
Atualmente, esse script funciona para a maioria das miniaturas e mostra-lhes na tela como desejado. No entanto, quando eu upload de uma imagem com uma largura de altura, dizer sobre 1000px, ele fica armazenado no banco de dados, mas o script não mostra a imagem resampled.
Para solucionar problemas, eu verifiquei o db, bem como print_r'd a saída da consulta. Tudo parece ser kosher ... Alguém poderia me dizer o que pode estar causando esse tipo de comportamento? Qualquer ajuda ou orientação é muito apreciada!
Solução
$ 5 diz php está ficando sem memória. Verifique seus logs de erro do PHP.