I'm developing a custom script to import photos from an FTP folder into Wordpress. Since I do not include wp-load
due to specific customer requests, I can not use the various classes like $ wpdb, etc ...
So I had to create the whole script, but I found a problem on resizing the photos with watermark applied...
This script picks up the photos in a folder, moves the original photo in high quality to a hidden folder to the public, then later, creates the copies in low resolution and applies the watermark above.
This is the primary function that cycle each photo contained in a folder:
function generate_resized_image($path_to, $path_from, $image, $sell_media_dir){
$upload_dir = wp_upload_dir();
$all_size = array(
'1' => array('width'=>150, 'height'=>150), //thumbnail
'2' => array('width'=>620, 'height'=>357), //medium
'3' => array('width'=>768, 'height'=>442), //medium_large
'4' => array('width'=>100, 'height'=>70), //very-small
'5' => array('width'=>300, 'height'=>200), //max-user-view
);
rename(str_replace(".jpg", ".JPG", $path_from), str_replace(".JPG", ".jpg", $path_to));
$newname = explode(".", $image);
foreach($all_size as $size){
//$path_to_save = $sell_media_dir."/".$newname[0]."-".$size['width']."x".$size['height'].".jpg";
$path_to_sell = $upload_dir['path']."/".$newname[0]."-".$size['width']."x".$size['height'].".jpg";
$img = resize_image( str_replace( ".JPG", ".jpg", $path_to ), $size['width'], $size['height'], $cut=($size['width'] == $size['height']) ? true : false);
imagejpeg($img, $path_to_sell, 10);
echo 'Immagine creata: ' . $newname[0]."-".$size['width']."x".$size['height'].".jpg ". PHP_EOL;
//rename($path_to_save, $path_to_sell);
}
$or_image = imagecreatefromjpeg( str_replace( ".JPG", ".jpg", $path_to ) );
imagejpeg($or_image, $upload_dir['path']."/".str_replace(".JPG",".jpg",$image), 10);
$img_size = getimagesize($upload_dir['path']."/".str_replace(".JPG",".jpg",$image));
return array(
'width' => $img_size[0],
'height' => $img_size[1]
);
}
This call a function resize_image
that, in theory, resize the images by applying the watermark above.
function resize_image($file, $w, $h, $crop=FALSE) {
$stamp = imagecreatefrompng('./wp-content/uploads/2018/11/spanshot_watermark.png');
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$w = imagesx($src);
$h = imagesy($src);
$stamp = PNGResize($stamp, $w, $h);
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// $par1 = (imagesx($src)/2)-($sx/2);
// $par2 = (imagesy($src)/2)-($sy/2);
imagecopy($src, $stamp, 0, 0, 0, 0, imagesx($stamp), imagesy($stamp));
echo "La lunghezza nuova è: " . $newwidth . PHP_EOL;
echo "L'altezza nuova è: " . $newheight . PHP_EOL;
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $src;
}
Finally, this function calls PNGResize
a resizing of the watermark while maintaining the transparency, which adapts the latter to the size of the image to be resized.
function PNGResize($image, $w, $h)
{
$oldw = imagesx($image);
$oldh = imagesy($image);
$temp = imagecreatetruecolor($w, $h);
imagealphablending( $temp, false );
imagesavealpha( $temp, true );
imagecopyresampled($temp, $image, 0, 0, 0, 0, $w, $h, $oldw, $oldh);
return $temp;
}
All this works correctly, except for the resizing of moveable images, which keep the original dimensions indifferently from the parameters, lowering the quality.
These are the original images in the right folder:
And these are the images, correctly moved, but not properly resized.
I do not understand why the images are not resized according to the past dimensions, can someone help me?