I'm trying to resize an image that is uploaded to Drupal via a form. The code I have for it is:
//Image resizing
//Get file and if it's not the default one - resize it.
$img = file_load($form_state['values']['event_image']);
if($img->fid != 1) {
//Get the image size and calculate ratio
list($width, $height) = getimagesize($img->uri);
if($width/$height > 1) {
$new_width = 60;
$new_height = $height/($width/60);
} else if($width/$height < 1) {
$new_height = 60;
$new_width = $width/($height/60);
} else {
$new_width = 60;
$new_height = 60;
}
//Create image
$image_p = imagecreatetruecolor($new_width, $new_height);
$ext = strtolower(pathinfo($img->uri, PATHINFO_EXTENSION));
if($ext == 'jpeg' || $ext == 'jpg') {
$image = imagecreatefromjpeg($img->uri);
} else {
$image = imagecreatefrompng($img->uri);
}
//Resize image
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//Save image as jpeg
imagejpeg($image_p, file_create_url($img->uri), 80);
//Clean up
imagedestroy($image_p);
//Store the image permanently.
$img->status = FILE_STATUS_PERMANENT;
}
file_save($img);
So what I'm trying to achieve is to save the new file (with the smaller size) over the old one that got uploaded.
The problem I'm getting is PHP throws a warning on imagejpeg($image_p, file_create_url($img->uri), 80);
saying:
Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost:8888/drupal/sites/default/files/pictures/myimage.png' for writing: No such file or directory in event_creation_form_submit()
Because of this, the image isn't resizing. Does anyone know what I'm doing wrong?
Thanks,