baranq baranq - 3 months ago 11
PHP Question

How can i get md5 from resized file before saving it to new file?

I'm doing resize on jpg, after that i would like to get md5 from it and then save new image with that MD5 name.
Code looks like that:

$extension = 'jpg';
$img = imagecreatefromjpeg($source);

$tmp_img = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $plik[0], $plik[1]);

//$md5 - here i need to get md5...

imagejpeg($tmp_img, $md5.;'.'.$extension);
imagedestroy($img);


Is it possible without saving that tmp file first?

Answer

Can you do at the top...

$md5 = md5_file($source);

I've also seen a technique where you stream the image file into an output buffer and capture that.

ob_start();
imagejpeg($tmp_img);
$data = ob_get_clean();

$md5 = md5($data);

You could then create your file with...

file_put_contents($md5 . '.' . $extension, $data);