79589641

Date: 2025-04-23 23:56:52
Score: 0.5
Natty:
Report link

try following for resizing the image:

<?php

$data = $form->getData();
$uploadedFile = $data['profielfoto'];

// get the original file info
$originalPath = $uploadedFile['tmp_name'];
$originalName = $uploadedFile['name'];
$targetDirectory = dirname($originalPath); // original dir

// create new filename
$info = pathinfo($originalName);
$resizedFilename = $info['filename'] . '_resized.' . $info['extension'];
$resizedPath = $targetDirectory . '/' . $resizedFilename;

// get dimensions from uploaded img
list($originalWidth, $originalHeight) = getimagesize($originalPath);

// calculate new dimensions
$percent = 0.5; // that would be 50% percent of the original size
$newWidth = $originalWidth * $percent;
$newHeight = $originalHeight * $percent;

$originalImage = imagecreatefromjpeg($originalPath);
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);

// resize and save
imagecopyresampled($resizedImage, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagejpeg($resizedImage, $resizedPath, 90); // 90 is quality (0-100)

// free up your memory
imagedestroy($originalImage);
imagedestroy($resizedImage);

// now $resizedPath should contain the path to the resized image

or if you want to crop the image heres some code for that too:

<?php

// define crop area
$cropWidth = 200; // your crop width
$cropHeight = 200; // desired crop height

// calc position (here: center)
$cropX = ($originalWidth - $cropWidth) / 2;
$cropY = ($originalHeight - $cropHeight) / 2;

// create the cropped image
$croppedImage = imagecreatetruecolor($cropWidth, $cropHeight);
imagecopyresampled($croppedImage, $originalImage, 0, 0, $cropX, $cropY, $cropWidth, $cropHeight, $cropWidth, $cropHeight);

// save cropped image
$croppedFilename = $info['filename'] . '_cropped.' . $info['extension'];
$croppedPath = $targetDirectory . '/' . $croppedFilename;
imagejpeg($croppedImage, $croppedPath, 90);
imagedestroy($croppedImage);

(used from my project and modified)

i hope that works for your code but please double check

Reasons:
  • RegEx Blacklisted phrase (1): i hope that works for your code but please
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Luis