I am working on a plugin, in plugin i create an upload meta fields for images to upload one or more images. Its working perfectly and in plugin settings i gave options to users that they set images size (widthxheight) for big image and thumbnail. First i use these options in <img />
attrribute width and height
and its resize the image but image not looking good.
So now i want to crop images by my own when wordpress upload images. First i try wordpress bult-in function add_image_size()
. It crop the images but the images not to assign to posts, because i use meta fields and the orginal images path store in wp_postmeta
table and the croped images path save in wp_posts
table but they not assign to any post (post_parent is 0)
I also use some core php code from net but i don’t understand that how can i merge this code in WordPress but its work in core php perfectly
Here is the core php code:
index.php
<form enctype="multipart/form-data" method="post" action="image_upload_script.php">
Choose your file here:
<input name="uploaded_file" type="file"/><br /><br />
<input type="submit" value="Upload It"/>
image_upload_script.php
$fileName = $_FILES["uploaded_file"]["name"];
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"];
$kaboom = explode(".", $fileName);
$fileExt = end($kaboom);
$moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");
include_once("ak_php_img_lib_1.0.php");
$target_file = "uploads/$fileName";
$resized_file = "uploads/resized_$fileName";
$wmax = 200;
$hmax = 150;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
ak_php_img_lib_1.0.php
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy);
In core php its work perfectly but i don’t know that how can i use this in wordpress or is there another way of wordpress to achieve this.