Jump to content

Resize image


Kenny Pollock

Recommended Posts

I'm trying to read a directory, for each image, resize it, then add a watermark to it and then delete the original. It's not adding the watermark, and it's making an image inside an image...

 

<?php

$directory = 'show_coverage/';

$handler = opendir($directory);

while ($file = readdir($handler))
{
if ($file != '.' && $file != '..')
{
	if ( !is_dir( $directory . $file . '/' ) )
	{
		$width = '600';
		$datename = date("Y-m-d-G-i-s")."-".rand(0,100);
		$target_path = $directory . $datename . '.jpg'; 
		list($width_orig, $height_orig) = getimagesize( $directory . $file );

		$height = ($width / $width_orig) * $height_orig;
		$image = imagecreatetruecolor( $width, $height );
		$image = imagecreatefromjpeg( $directory . $file );
		imagecopyresampled( $image, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig );  
		imagejpeg($image, $target_path, 100);

$watermark = imagecreatefrompng('flminiswatermark.png');  
$watermark_width = imagesx($watermark); 
$watermark_height = imagesy($watermark);
$image2 = imagecreatetruecolor($watermark_width, $watermark_height);
$image2 = imagecreatefromjpeg($target_path);
$size = getimagesize($target_path);  
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;  
imagecopymerge($image2, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);  
imagejpeg($image2, $target_path, 100);  

		unlink( $directory . $file );
		echo '<img src="' . $directory . $datename . '.jpg" alt="" /><br /><br />';			
	}
}
}

closedir($handler);

?>

Link to comment
https://forums.phpfreaks.com/topic/67397-resize-image/
Share on other sites

Maybe its me but your code looks a bit messed up. A simple merging of two png's should be like:

 

$image = imagecreatefrompng('image.png');
$watermark = imagecreatefrompng('watermark.png');
imagecolortransparent($watermark, imagecolorat($image, 0, 0));
imagecopymerge($image, $watermark ,0 ,0 ,0 ,0 , 100, 50, 100); 
header('Content-type: image/png');
imagepng($image , "", 100);

 

I did this code from a tutorial some time a go and it worked greatly. Maybe u can consider its basic usage.

Link to comment
https://forums.phpfreaks.com/topic/67397-resize-image/#findComment-338373
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.